Yeah, well, this question seems to pop-up once in a while until a got the hang of namespaces (native ones) in ZF. Here's my take on this. This is for all you out there who just want to proper load some third party that's using namespaces. It's dead simple.
I'm using ZF 1.11.11
(as per documentation, all ZF versions 1.10+ work).
First of all, as of 1.10, ZF supports native PHP namespaces autoloading, provided they conform to the PSR-0 standard.
I wanted to add the Symfony2 EventManager component in a ZF1 project.
First off all, like with the class names, the namespace must match with the path in library.
So, the namespace Symfony\Component\EventDispatcher\EventDispatcher
maps to path/to/lib/Symfony/Component/EventDispatcher/EventDispatcher.php
(where path/to/lib/
is APPLICATION_PATH . '/library'
; you get the idea). Must I mention that the library folder must be in include_path? No, I guess I don't.
Now with the-not-so-tricky-part:
<?php
// bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initNsAutoload()
{
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Symfony'); // this is why is dead simple
/* Don't believe me? Try it:
Zend_Registry::set('events', new Symfony\Component\EventDispatcher\EventDispatcher());
*/
}
}
How about using namespace imports in controllers?
<?php
use Symfony\Component\EventDispatcher\Event,
Symfony\Component\EventDispatcher\EventDispatcher;
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
var_dump(new EventDispatcher());
var_dump(new Event());
}
}
So, as long as you're on ZF 1.10+, there's no need for a custom autoloader. This reply was made after I peeked at this.
LE: or add this to application.ini:
autoloaderNamespaces[] = Symfony