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?

图片转代码服务由CSDN问答提供 功能建议

基本上,我想使用Composer自动加载器(用于加载第三方库),但我想要 继续在Zend 1.12中使用内置机制进行自动加载

我添加了以下代码:

 &lt;?  php //文件路径:index.php 
 
 // ... 
 
 $ composerAutoloaderPaths = array(
'../vendor/autoload.php',
'../../common  /vendor/autoload.php'//存储多个项目使用的公共库,目前通过在set_include_path()
)中添加目录来工作; 
 
foreach($ composerAutoloaderPaths as $ composerAutoloaderPath)
 {
 if(  file_exists($ composerAutoloaderPath))
 {
 require_once $ composerAutoloaderPath; 
} 
 else 
 {
 //正常处理错误
} 
} 
 
 // ... 
 <  / code>  
 
 

另外,我正在使用 Zend_Loader_Autoloader ,如下所示:

 &lt;?php /  /文件路径:Bootstrap.php 
 
 // ... 
 
 $ autoloader = Zend_Loader_Autoloader:  :getInstance(); 
 
 $ autoloader-&gt; registerNamespace('Plugin _'); 
 $ autoloader-&gt; registerNamespace('Helper _'); 
 //等等
 
 // ...  
   
 
 

使用像这样的Composer和Zend自动加载器有什么值得担心的吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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

相关推荐 更多相似问题