Jump to content

spl_autoload and namespaces


Jerzxu

Recommended Posts

I did a search on the forums before posting this So I have been attempting to use spl_autoload_extensions('.php') to autoload my classes based off namespaces but it continually says that the namespace does not exist.

 

Code I am using:

 

spl_autoload_extensions('.php');
spl_autoload_register();

use Test\Systems\User as User;

$usr = new User();

 

The error I get is this: Class Test\Systems\User could not be loaded in..

 

My files are exactly as named in the namespace. So

Test > Systems > User.php

 

I have also tried variations of that like:

spl_autoload_extensions('.php');
spl_autoload_register();
use Test\Systems as Test;
$usr = new Test\User();

And so on. Nothing appears to be working.

 

I looked around online and seen that at one point *nix systems couldn't use autoloading but apparently php 5.3.3 fixed this functionality. The server currently is running on 5.3.13 so I am not sure why this isn't working.

 

Any insight would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/263921-spl_autoload-and-namespaces/
Share on other sites

spl_autoload_register expects a function that will be used to resolve your class name to a file to include. Your not passing it anything.

 

I find it kind of weird that spl_autoload uses this functionality for namespaces. Numerous places online I've read mentioned that spl will look at the namespace and attempt to load it based on the namespace as if it were a directory structure.

 

http://www.dreamincode.net/forums/topic/148935-php-53-namespacing-and-autoloading/ Example of autoloading with namespaces. Though this is done on windows though, not linux.

 

Guess I will just specify a function to load it. Thanks.

Numerous places online I've read mentioned that spl will look at the namespace and attempt to load it based on the namespace as if it were a directory structure.

 

spl does nothing of the sort. it's up to *you* to write your autoloading mechanism. The spl_autoloader is just there to help you register that mechanism.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.