Jump to content

Recommended Posts

I saw a tutorial on namespaces and got the idea it is a way to group all your classes together to I went ahead and rearranged my whole site and now have all my classes as part of a namespace. But I don't know what to do with them now. The namespace is declared in lib/solutions in a file called solutions.inc.php and all my classes are stored in subfolders of this solutions directory, and they are all in subnamespaces with the same name as the subdirectory. For example my database and config class are in the config folder so they are in the \solutions\config namespace.

 

Alright, so firstly I have to be able to load the classes, so I try:

include ('lib/solutions/solutions.inc.php'); // <--- thats where the parent (solutions) namespace is defined

$path = '\solutions\config\db';

$db = new $path();

to load my database class (which is stored in db.php), but I get:

 

Fatal error: Class '\solutions\config\db' not found in /var/www/questions/index.php on line 14

 

 

so I'm thinking now the problem must be that the db.php file hasn't actually been included (I thought namespaces were gonna eliminate the need to include class files individually).

 

So I came across people talking about these autoload functions which sound great. I read that the __autoload() function is obsolete so I wanna use the spl_autoload functions. But ther a few things I'm not understanding. Firstly I pointed spl_autoload_register(); directory to one of my class files but left out the file extension so like this: spl_autoload_register('/path/classfile'); and I got an error:

 

 

Fatal error: Uncaught exception 'LogicException' with message 'Function 'config/db' not found (function 'config/db' not found or invalid function name)' in /var/www/questions/lib/solutions/autoload.php:12 Stack trace: #0 /var/www/questions/lib/solutions/autoload.php(12): spl_autoload_register('config/db') #1 {main} thrown in/var/www/questions/lib/solutions/autoload.php on line 12

 

got the same problem is add the fie extension. I'm confused. I thought this was supposed to register a group of class files and include them whenever you try to start an instance of one of the classes in them. I even tried it with this function:

spl_autoload_extensions('.php,.inc,.class.php,.lib,.lib.php');
why would it need a list of file extensions unless it includes file?

Edited by CrimpJiggler
Link to comment
https://forums.phpfreaks.com/topic/285019-namespaces-and-autoload-i-need-help/
Share on other sites

Alright, I see that parameter 1 for spl_autoload_reigster has to be a callback function which includes a file, so I tried this:

function autoload_test($config_instance) {
	include 'lib/solutions/config/config.php';
}
spl_autoload_register('autoload_test');

and I also figured out it'll only work if I add an instance of the class as the parameter. The more I figure out, the more confused I get. So what the hell does spl_autoload_register actually do? Does it somehow memorise every class instance I register with it, then next time I wanna instantiate that class, I don't have to both including the file containing the class, is that it? If so, then what do spl_autoload and __autoload do?

So many questions in your post. You are lost.

 

 

 

I thought namespaces were gonna eliminate the need to include class files individually

 

Nope, they do nothing of the sort.

 

You cannot simply include a namespace. Im not sure where your getting your information but you are misinterpreting it completely.

 

Before you go too much further, you should be aware that there are standards for this type of thing. You should just stick to one of the standard implementations, it will make things easier in the future. If you use [url=https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md]PSR-0 for instance you can use [url=http://getcomposer.org]Composer's autoloader to load your libraries ([url=https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md]PSR-4 has recently passed too but hasn't yet been adopted by Composer). If you don't know what Composer is, you need to find out.

I figured it out. Turned out to be completely useless in my case since I don't have a problem with class name collision. I hope in future versions of PHP they'll add some features that make namespaces a bit more useful. Like something which registers the names and locations of every file in a particular namespace so that you can include them just by having the main namespace included.

so that you can include them just by having the main namespace included.

If you follow the established standards you can already include classes simply by referencing them with the default autoloader implementation. If you call spl_autoload_register() without giving it a function it will use a default implementation which basically just treats the namespace as a directory path. Set your include path to contain the root of your files and have a file/directory structure matching your namespace layout and PHP will automatically include them.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.