开发者

How do I allow autoloading of custom libraries in 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('_', '/', 开发者_StackOverflow社区$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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜