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 becausespl_autoload_register()
will effectively replace the engine cache for the__autoload
function by eitherspl_autoload()
orspl_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.
精彩评论