PHP 5.4 trying to implement namespaces in project - php

I have a php 5.4 application where I am trying to implement namespaces.
I have a models folder with a model Admin.php in it.
<?php
namespace models;
class Admin extends Model
{
public function getAllContent($lang)
{
return some stuff ...
}
}
I have an adminController.php where I do this:
<?php
use models\Admin;
class adminController extends Controller
{
function index()
{
$variable = Admin::getAllContent($this->lang);
}
}
I get this error
Fatal error: Class 'models\Admin' not found in /.../controllers/adminController.php on line 22
this is line 22 : $variable = Admin::getAllContent($this->lang);

Related

Class 'App\TestService' not found - Laravel

Problem
I created service TestService, that I use in colntroller file TestController in function test().
When I called test(), I got an error:
local.ERROR: Class 'App\TestService' not found {"userId":1,"exception":"[object] (Error(code: 0): Class 'App\TestService' not found at /Backend/app/Http/Controllers/TestController.php:8)
Code
TestController.php:
<?php
namespace App\Http\Controllers;
use App\TestService;
class TestController extends Controller
{
public function test()
{
return response()->json(TestService::getTest());
}
}
TestService.php:
<?php
namespace App;
use App\TestService;
class TestService
{
public static function getTest()
{
return "test";
}
}
What I tried
I checked all the names and they are correct.
When I wrote in the colntroller file use App\TestService;
I had autocomplete, so the service and name are visible.
I used these commands to refresh the files: php artisan serve and php artisan clear-compiled.
But it still doesn't work.
You have not correctly defined Namespace.
The namespace must be a directory path where you have created a file.
TestService.php:
<?php
namespace App\Services\Tests;
class TestService
{
public static function getTest()
{
return "test";
}
}
TestController.php:
<?php
namespace App\Http\Controllers;
use App\Services\Tests\TestService;
class TestController extends Controller
{
public function test()
{
//Call service class like.
return response()->json(TestService::getTest());
}
}

Yii 2 - using custom helpers

I'm trying to use a custom helper class which i create under frontend/components/Helper (Helper.php)
The content of that file is something like:
<?php
namespace frontend\components\Helper;
class Helper {
public static function helperGreetings() {
echo("hello helper");
}
}
?>
and on my SiteController.php i have the following:
use frontend\components\Helper;
class SiteController extends Controller
{
public function actionIndex()
{
Helper::helperGreetings();
return $this->render('index');
}
}
What should i do to have it working?
BTW, the error i get is Unknown Class – yii\base\UnknownClassException
Unable to find 'frontend\components\Helper' in file: /Users/foo/sites/bar.dev/frontend/components/Helper.php. Namespace missing?
Change the namespace in the Helper class from
namespace frontend\components\Helper;
to
namespace frontend\components;

CI_Model not found in extending class

I can't instantiate my models manually on my controller using require_once, following is the problem:
Controller
<?php
require_once './application/models/portion/portioncreatormodel.php';
class Payment extends CI_Controller {
function sample() {
$pc = new PortionCreatorModel();
echo 'OK';
}
}
Model
<?php
class PortionCreatorModel extends CI_Model {
function __construct() {
parent::construct();
}
}
When I load this model using $this->load->model('portion/portionCreatorModel') it works perfectly (in controllers or other models), when I load using require_once using this very same require_once './application/models/portion/portioncreatormodel'; it works perfectly in other models.
Why doesn't it work properly on controllers? I found this in my error.log:
PHP Fatal error: Class 'CI_Model' not found in /var/www/html/myerp/igniter/application/models/portion/portioncreatormodel.php on line 3
Note: I already tried this require_once:
require_once APPPATH.'/models/portion/portioncreatormodel.php';
Please, don't tell me the only way to solve it is renaming PortionCreatorModel to Portioncreator_Model, it is already working properly on models, the problem is on controllers.
Any glues?
Try using this in your controller:
<?php
class Payment extends CI_Controller {
function sample() {
$this->load->model('PortionCreatorModel', 'pc');
// Reference by using $this->pc
echo 'OK';
}
}
Reference: CodeIgniter: Models

Identical class name in different files phpunit conflict

When i test my application on Zend Framework 2 i have strange error. For example, i have 2 phpunit test files:
1:
namespace Test\Model;
class Test1Test extends \PHPUnit_Framework_TestCase {
public function test1test() {
$this->assertTrue((new \Austero\Model\Test1())->t1());
}
}
2:
namespace Test\Model;
class Test2Test extends \PHPUnit_Framework_TestCase {
public function test1test() {
$this->assertTrue((new \Austero\Model\Test2())->t2());
}
}
And 2 tested model files:
namespace App\Model;
class Test1 {
public function t1() {
return true;
}
}
And:
namespace App\Model;
use
\App\Controller\Test1; // THERE USE FILE FROM ANOTHER NAMESPACE WITH SAME NAME THAT Test1 FROM App\Model NAMESPACE
class Test2 {
public function t2() {
(new Test1())->t3();
return true;
}
}
Then i got:
PHP Fatal error: Cannot use App\Controller\Test1 as Test1 because the name is already in use in /home/user/projects/app/module/App/src/App/Model/Test2.php on line 5
How can i avoid this, without change used alias? Thanks for answer

Namespace is not working in contoller

I have created Mycontoller.php inside app/controllers
class MyController extends BaseController {
public function showWelcome($name){
return "Hello $name !! ";
}
}
When i use inside routes.php
Route::get('user/{name}','App\Controllers\MyController#showWelcome');
It is giving exception
ReflectionException Class App\Controllers\MyController does not exist
just use MyController#showWelcome With the code you showed, you don't have any defined namespaces. You should make it
Route::get('user/{name}','App\Controllers\MyController#showWelcome');

Categories