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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.