聘我网

新概念招聘3.0

PHP中autoload实现机制是什么?

vote up0vote downstar
spl_autoload_register(array('Doctrine', 'autoload'));
spl_autoload_register(array('Doctrine2', 'autoload'));

假设register了上面多个autoload函数,PHP会如何查找?

 

2 个答复

vote up0vote downcheck

run through each of them in the order they are defined

参考php.net/splautoloadregister

bool spl_autoload_register  ([ callback $autoload_function  [, bool $throw = true  [, bool $prepend = false  ]]] )

函数定义中的callbackPseudo-type,上面的用法是对应这条:

Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object at index 0.

即第一个元素为类名,第二个为静态方法名

更新

还有一种调用方法似乎更简单,直接定义一个__autoload($className)函数即可:

function __autoload($class)
{
    echo 'win!';
}

new nocush;
链接
vote up0vote down

spl_autoload_register的优先级更高一些

链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.