dongshi949737 2017-04-21 11:09
浏览 52
已采纳

在Zend 1.12中使用Composer自动加载器(用于加载外部库)?

Basically, I'd want to use Composer auto-loader (for loading third-party libraries), but I want to continue using built-in mechanism for auto-loading in Zend 1.12

I added the following piece of code:

<?php // File path: index.php 

// ...

$composerAutoloaderPaths = array(
    '../vendor/autoload.php',
    '../../common/vendor/autoload.php' // store common libraries used by multiple projects, currently that's working by adding the directory in set_include_path()
);

foreach($composerAutoloaderPaths as $composerAutoloaderPath)
{
    if(file_exists($composerAutoloaderPath))
    {
        require_once $composerAutoloaderPath;
    }
    else 
    {
        // handle the error gracefully
    }
}

// ...

Also, I'm using Zend_Loader_Autoloader like this:

<?php // File path: Bootstrap.php 

// ...

$autoloader = Zend_Loader_Autoloader::getInstance();

$autoloader->registerNamespace('Plugin_');
$autoloader->registerNamespace('Helper_');
// etc.

// ...

Is there something to worry about using Composer and Zend autoloaders like this?

  • 写回答

3条回答 默认 最新

  • donglingsai2880 2017-04-24 15:05
    关注

    I often encounter this problem and I believe it's not an actual problem.

    The best way IMO is to just include the composer autoloader in public/index.php as it's done in ZF2/3. This will not change a thing about the rest of the autoloading : https://github.com/zendframework/ZendSkeletonApplication/blob/master/public/index.php#L21

    Beware: if you used another entry point in your application (for cron scripts for instance), you'd need to add the same lines (basically in each entrypoint of your application).

    Also, if you look at the rules in phpmd, one gives this message:

    A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both.

    Therefore, declaring the include of the vendor autoloader in the bootstrap could be considered as a malpractice (at least seems to be a shared opinion between whoever wrote this rule and myself :)).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?