duanshan3427 2011-08-22 03:16
浏览 61
已采纳

如何在CakePHP中允许自动加载自定义库?

Hi I am using CakePHP and there are some Vendor files I need to add to the autoloading. Is this the correct method:

In the bootstrap.php, I have the following code:

function __autoload($className){

        $classFile = '../vendors/phprtf/' . str_replace('_', '/', $className) . '.php';

        // check if file exists
         if(file_exists($classFile)) {
            require $classFile;
        }

}

The PHPRTFLite has a lot of class files under various sub directories. So listing all of them is not a good option.

It seems to be working for now.

  • 写回答

1条回答 默认 最新

  • dongma6326 2011-08-22 03:18
    关注

    You should use spl_autoload_register() for the flexibility it offers.

    If your code has an existing __autoload function then this function must be explicitly registered on the __autoload stack. This is because spl_autoload_register() will effectively replace the engine cache for the __autoload function by either spl_autoload() or spl_autoload_call().

    If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?