Lately I came across the nice little autoloader provided by the Zend Framework. So I like to share some experiences. First of all, like I mentioned in the “About” section of my page, to me Zend is more like a new PEAR, but not like a framework to build websites. This is why I use it together with some other php frameworks and CM-Systems.
But back to the topic of this post,.. I came across the new Autoloader Class provided with the Zend Framework, studied some of the documentation. And Failed,.. perhaps I over-read something but where does the documentation mention that your class has to be prefixed with the name of the namespace one registers ?
But one by one, if you like to use the autoloader either with your extension to the CM-System of your choice (in my case this would be Typo3 at 90% of the projects I work on), or within some php-code you write for a customer, you will have to start wit something like this:
set_include_path ( '.' . PATH_SEPARATOR . 'path/to/Zend_Framework/' ); require_once 'Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance (); $autoloader->setFallbackAutoloader ( true ); $autoloader->registerNamespace ( 'nameOfYourNameSpace' );
This was how far I got with the documentation. Because I know some other languages like JAVA, I knew that I would have to create a nice little sub folder called “nameOfYourNameSpace” in the directory for a SOAP – Client I lately wrote for a customer of mine. I went on creating the class file I needed:
class myClass
{
public function __construct()
{
// here the code Starts
}
}
Like I said I am used to the conventions of other languages when it comes to namspaces, so I called the file myClass.php. Now in my imprudence I thought something like this would work right from the Start, as well as I am getting rid of those require_once statements:
$o_myObject = new nameOfYourNameSpace_myClass();
Hhmmm,… nope it doesen`t,… turns out that the class within the file has to be prefixed by the name of the namespace it self. So I had to change the php class to this:
class nameOfYourNameSpace_myClass
{
public function __construct()
{
// here the code Starts
}
}
After that, the first code segment worked fine for me, and I had a nice little autoloader to load my models. Is it just me, or would you too have expected that it should be enough to create the folder structure registered in the first snippet and call the class just like the file name ?
Perhaps this was a little help for you getting to know the Zend_Loader_Autoloader.
Drop me some thoughts if you like.

