I have this autloading struggle with the Zend Framework. Basically there is a folder named LunaZend in library folder. LunaZend has some classes which can be used in Zend Framework and these classes have namespaces and must be loaded automatically only by calling namespace names. Namespaces are like LunaZend\DB, LunaZend\Etc ... In bootstrap I have an _initAutoLoadNS function which has
$resource = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH.'/../library/LunaZend/',
'namespace' => 'LunaZend')
);
I want to be able to load classes in Zend Framework only by calling like
$t = new LunaZend\Di\DependencyInjector();
However I get the error
Fatal error: Class 'LunaZend\Di\DependencyInjector' not found in... What am I doing wrong? How to deal with this namespace autoloading issue?
Thank you.
You can use application.ini to load some namespace.
I used the same
Following can be application.ini' sample code:-
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 topath/to/lib/Symfony/Component/EventDispatcher/EventDispatcher.php
(wherepath/to/lib/
isAPPLICATION_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:
How about using namespace imports in controllers?
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
AFAIK, the ZF1 autoloader does not handle genuine PHP 5.3 namespaced classes. To use the ZF1 autoloader on true namespaced classes, you need to configure the separator variable to be\
. (Thanks to @Mattieu for the correction). ButZend_Loader_Autoloader_Resource
doesn't handle the path mapping the way we might expect.You could use a namespace-aware autoloader, like that of Doctrine2 or ZF2. Pushing one of these autoloaders onto the standard ZF1 autoloader stack should handle it.