Trouble with Namepaces and Class Loading - php

Im building a custom CMS and have setup an autoloader, and have adapted use of namespaces. For the most part things are loading properly, but in certain cases PHP reports that it cannot find the class, the class file has been included.
Once a file is included (using require), it should be instanced as well.
The parent controller is instanced, then the child controller attempts to instance a few of its own dependencies.
$this->auth = new \Modules\Auth\RT\Auth();
This will look for a file at /modules/auth/rt/auth.php, and it does and the class is instanced properly.
The namespace of Auth is:
namespace Modules\Auth\RT;
The auth class tried to load its own dependencies, a model in particular.
$this->auth_model = new Models\ModelAuth();
Here the file to be included is at /modules/auth/rt/models/modelauth.php
It is included successfully, but this is where PHP says I cannot find this class.
Fatal error: Class 'Modules\Auth\RT\ModelAuth' not found in /Users/richardtestani/Documents/ShopOpen-Master/shopopen/modules/auth/rt/auth.php on line 12
What would cause the class from not being instanced even though the file is included?
Thanks
Rich

try this:
$this->auth_model = new Modules\Auth\RT\Models\ModelAuth();

try this:
$this->auth_model = new \Modules\Auth\RT\Models\ModelAuth();
OR
$this->auth_model = new Models\ModelAuth();
when you are in this namespace \Modules\Auth\RT
There was a Missing \ , so the code trys to include the namespace twice;
FOR REAL ;)

Related

Namespace conflict when using 3rd party api in class

There is something weird going on. I'm a novice with respect to php programming.
I'm trying to use FirePHP but my question is actually not related to that tool directly.
The function fb you'll see below is part of FirePHP. My entry point is Main.php. The first fb call is executed without any problems but the second call (see ExperController.php) ,which gets triggered when $ec->exper(); in Main.php is called, causes a fatal error:
Fatal error: Uncaught Error: Class 'App\Controllers\FirePHP' not found in path-to-wp-directory\wp-content\mu-plugins\typerocket\app\Controllers\ExperController.php on line 12
Why is the runtime engine looking for the class FirePHP under that namespace (App\Controllers)? And why wasn't this an issue during the first fb call? There isn't any namespace defined in the FirePHP files at all. And my last but crucial question, how can I fix this without having to touch 3rd party files? This answer is not a solution for my case since I'm not referencing / calling the mentioned class in my code!
Note: FirePHP is included via autoloader (provided by composer).
Main.php:
<?php
fb('Hello World!', FirePHP::INFO);
$req = new \TypeRocket\Http\Request();
$res = new \TypeRocket\Http\Response();
$ec = new \App\Controllers\ExperController($req, $res);
$ec->exper();
ExperController.php:
<?php
namespace App\Controllers;
use TypeRocket\Controllers\Controller;
class ExperController extends Controller
{
public function exper() {
fb('Hello World!', FirePHP::INFO);
}
}
do you use any Dependency manager like composer??
when you call it from main somehow php can see where is FirePHP class is and everything works fine but when you try to access it inside a class in another folder you must define where the class is inside your controller , like how you defined where is controller class that you extend :
use TypeRocket\Controllers\Controller;
so add FirePHP class to your controller and everything will be ok then.
use Path\To\Class\FirePHP;
also if you did all and still not working you may try dump autoload;
composer dump-autoload

Laravel namespace issue

I have a laravel 4.1 application, and I've created a folder in my app folder to store most of the logic.
/app/Acme/Models/
/app/Acme/Repositories/
these are the two main folders.
In my composer.json I have this in the auto load, and done a dump run.
"psr-4" : {
"Acme\\" : "app/Acme"
}
However I am getting, what I think are silly issues. For example my Acme/Models/Task.php has the following
<?php
namespace Acme\Models;
class Task extends \Eloquent {
public function job()
{
return $this->belongsTo('Job');
}
}
however when I run this, I get an error
Fatal error: Class 'Task' not found in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 780
In my Job.php I have the same namespace at the top of the file....
Must I manually import/use object which are in the same name space?
use Acme/Models/Job as Job; ? it seems like such a duplicate...
And in my Repositories folder when I set a namespace of namespace Acme/Repositories;, must I use items like
use Acme\Models\Job as Job;
I is, a bit lost!
Namespaces are relative so you do not need to add use to directly reference classes within the same namespace.
The error you are getting is because you need to fully qualify relationships to namespaced models so eloquent knows where to find them eg
$this->belongsTo('\Acme\Models\Jobs');
In the case of your repository namespace, you will need to add a use statement in your file as you suggested, or reference the fully qualified namespace eg new \Acme\Models\Job();
On a side note, I know PHPStorm (and I'm sure other IDEs) will inject the namespaces for you which is super useful and saves you having to write use or the full namespace out every time you reference a class - worth checking out.
Edit: Sorry, I didn't read the question properly first time around - updated my answer.

Using namespaces in PHP

I am working on the AWS documentation which uses Guzzle framework. I have to deal with namespaces here and I am not able to get it working. I went through the docs and examples and understood that we can have packages for projects using namespaces.
I went ahead and tried a simple example, but unsuccessful. Here's the example: this is the index.php:
use My\Full\Classname as Another; //Also tried use My\Full\Classname
$obj = new Another; //with $obj = new Classname;
echo $obj->add();
I have Classname.php in the directory structure like this My->Full->Classname.php:
<?php
class Classname{
public static function add(){
return 2+2;
}
}
?>
I am trying to call the function in index.php but getting error:
Fatal error: Class 'Another' not found in C:\wamp\www\guzzleEx\index.php on line 19
which is the line where I instantiate the Classname object $obj = new Another;
What is the mistake i am making? Is there any INI that needs to be updated or any other config issue? How can I make the code working? If you use the normal include for Classname.php it works fine.
Namespaces need to be explicitly declared, they do not come from a certain directory structure.
So if you do not have a line that reads namespace My\Full; in front of your class Classname, then your class is not in any namespace, but in the root namespace.
Thus you cannot use it as \My\Full\Classname, but \Classname or even Classname directly.

functions accessible by multiple php classes (Symfony controllers)

I have a couple functions that are used in multiple controllers in my Symfony project. Instead of copy and pasting them constantly, I'd like to have a separate php file (say functions.php) to store the functions.
I've tried the following:
1) include and require the php file with the functions, but it won't let me use $this->getDoctrine().
I've looked at the following posts for help, but it didn't solve the issue:
Symfony2: get Doctrine in a generic PHP class
Symfony2 Use Doctrine in Service Container
2) I've tried making it a class and having the functions as methods then doing:
$f = new Functions();
$variable = $f->myMethod();
But it returns:
The file was found but the class was not in it, the class name or namespace probably has a typo.
Thanks for any help. I really appreciate it.
UPDATE:
The generic class is Symfony/src/WikiRoster/MainBundle/Controller/DBFunctions.php
I just need to be able to use $this->getDoctrine() or the like somehow in there now. I've tried using the services.yml as suggested in the link above, but no cigar. Thanks!
The file was found but the class was not in it, the class name or
namespace probably has a typo.
This issue happens quite often actually. As the error mentions, there are 2 cases explaining why this error is returned:
If the namespace of your file is not the same as the path of your file
If the name of your file is not the same as the name you are using in your class
Example
Let's say you would like to use a GeneralClass in a different class like that:
use Acme\YourBundle\General\GenericClass
For this to work, GeneralClass needs to be in Acme/YourBundle/General/, and you need to:
use the correct namespace: namespace Acme\YourBundle\General
name your class with the same name: class GenericClass
You would have:
namespace Acme\YourBundle\General;
class GenericClass
{
public function __construct()
{
// some cool stuff
}
}

Codeigniter extending exception class

I am trying to load a custom exception class that I created according to the instructions here:
http://codeigniter.com/user_guide/general/core_classes.html
MY_Exceptions.php is stored at application/core/
Somehow, when I try loading it, I keep getting this error:
Fatal error: Class 'MY_Exceptions' not found in C:\xampp\htdocs\xampp\facebook\application\models\campaign_model.php on line 29
Based on the instructions, it doesn't say I have to autoload the class or anything. What am I doing wrong?
MY_Exception.php is stored at application/core/
Name the class and file MY_Exceptions with an s
You do not need to autoload or manually load anything in the core directory, nor should you. They are required classes for CI to run that are automatically loaded.
For creating core classes, use this documentation instead: http://codeigniter.com/user_guide/general/core_classes.html
Keep in mind that when calling the class you will use the original class name. Let's say you have created MY_Input. Example:
$this->input->post(); // Do this
$this->my_input->post(); // Don't do this
To understand the why and how, see system/core/Common.php function load_class.
Looking at the docs now, I agree that this should probably be highlighted.
It's trying to find MY_ExceptionS.php, not MY_Exception.php.

Categories