Zend Resource Autoloaders not working for namespaces

前端 未结 3 1886
南旧
南旧 2021-01-13 21:09

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 Fr

相关标签:
3条回答
  • 2021-01-13 21:40

    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

    0 讨论(0)
  • 2021-01-13 21:56

    You can use application.ini to load some namespace.

    I used the same

    Following can be application.ini' sample code:-

    [production]
    phpSettings.display_startup_errors = 0
    phpSettings.display_errors = 0
    appnamespace = "Application"
    
    ;Autoloader Namespace
    autoloaderNamespaces.w = "LunaZend_"
    
    0 讨论(0)
  • 2021-01-13 22:02

    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). But Zend_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.

    0 讨论(0)
提交回复
热议问题