LAMPlights Personal anecdotes from my experiences using the LAMP stack

25Oct/094

Hidden features with spl_autoload() and namespaces

The namespace operator in PHP 5.3 is a backslash (\). One of the criticisms of this operator is that the code starts to look like directory paths on Windows. The added side benefit of this is that spl_autoload() knows how to autoload classes that use a namespace style that matches the directory layout.

$baz = new \foo\bar\Baz;

The spl_autoload() function gets passed the fully qualified namespace as well as the class name. This is very similar to the PEAR class naming convention of using underscore characters (_) to denote the path to a class.

Consider the following:

The spl_autoload() funtion is passed the string 'foo\Bar'. The extensions registered by spl_autoload_extensions() are then used in conjunction with include paths to look for a valid file. The spl_autoload_extensions() function has the extensions .inc and .php registered by default. The default include_path is the current directory, so let's assume the current directory is D:\herman\php.

List of attempts by spl_autoload() to load the class from the above example:

  • D:\herman\php\foo\Bar.inc
  • D:\herman\php\foo\Bar.php

This means that you can map each namespace to a directory in your application. Consider the following directory layout.

blog\
models\
Author.php
controllers\
PostController.php

The namespace for the Author class in the Author.php file should be 'blog\models'. The namespace for the PostController class in the PostController.php file should be 'blog\controllers'. Using this namespacing strategy along with a proper include path allows php to autoload your classes out of the box.

Remember, this only works with PHP on Windows.  I have a pending patch to make spl_autoload() work with namespacing by default on Linux.

Comments (4) Trackbacks (0)
  1. Have you come out with the patch yet? I’m building a framework where I use the spl_autoload trick but it only works on windows. In my code I have:

    spl_autoload_extensions(“.php”);
    spl_autoload_register();

    And this is wonderful since it autoloads all of the nampspaced components very easily in the framework.

  2. I sent to it internals, but no one seemed to care. I will try and send it again and see there is a more positive response this time.
    Twitter:

  3. I hope they care, because I’m building a framework http://trac.andydaykin.com and it would save me from building a loader app which would be a great help to me, since in windows I can automatically load all of my namespaced classes using the code above I posted.

    Have they sent anything back yet?

  4. Hi, I tried to find someplace to follow your patch, but couldn’t find any.
    Do you perhaps have an url for me so I can track its progression?
    I also notified some other people of your patch :
    http://stackoverflow.com/questions/2862133/namespace-autoload-works-under-windows-but-not-on-linux/2893956#2893956
    /me=hoping it’ll be patched soon.
    Twitter:


Leave a comment


No trackbacks yet.