Jerzxu Posted June 9, 2012 Share Posted June 9, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 9, 2012 Share Posted June 9, 2012 Did you set the namespace in the class file? Quote Link to comment Share on other sites More sharing options...
Jerzxu Posted June 9, 2012 Author Share Posted June 9, 2012 Did you set the namespace in the class file? Forgot to post that. Class file looks like this: namespace Test\Systems { class User { #Class functions etc. } } Quote Link to comment Share on other sites More sharing options...
trq Posted June 10, 2012 Share Posted June 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jerzxu Posted June 10, 2012 Author Share Posted June 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
trq Posted June 10, 2012 Share Posted June 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.