【问题标题】:using multiple autoloaders php使用多个自动加载器 php
【发布时间】:2012-10-31 23:26:18
【问题描述】:

您好,我正在尝试将 SILEX 微框架与我自己的充满类的库一起使用,因此我遇到了 2 个加载器,导致加载器无法加载类的错误。有没有办法使用这些2 个加载器同时没有出现此错误?

我使用的加载器可以在下面找到:

    <?php

/*
 * Loader
 */

function my_autoloader($className) 
{
// haal de base dir op.
  $base = dirname(__FILE__);


  // het pad ophalen
  $path = $className;

  // alle paden samenvoegen tot waar ik zijn moet en de phpfile eraan plakken.
  $file = $base . "/lib/" . $path . '.php';       

  // als file bestaat haal op anders error
  if (file_exists($file)) 
  {
      require $file;
  }
  else 
  {
      error_log('Class "' . $className . '" could not be autoloaded');
      throw new Exception('Class "' . $className . '" could not be autoloaded from: ' . $file); 
  }
}

spl_autoload_register('my_autoloader');

?>

silex 使用的加载器位于供应商目录中(来自框架本身)

这就是我的文件树的样子:

【问题讨论】:

    标签: php autoloader


    【解决方案1】:

    不要在您的自动加载器函数中抛出错误。 spl_autoload_register 允许 php 按顺序遍历所有已注册的自动加载器,但如果您在该过程中间抛出未捕获的错误,则无法尝试下一个自动加载器。

    http://php.net/spl_autoload_register

    【讨论】:

    • 嘿,成功了!谢谢 :) 尽可能接受您的正确答案(即 4 分钟)
    猜你喜欢
    • 2013-05-16
    • 2012-11-08
    • 2012-11-06
    • 1970-01-01
    • 2012-03-14
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    相关资源
    最近更新 更多