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条)

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?