spl_autoload_register 如何处理数组而不是函数名?
How do the spl_autoload_register do with array not function name?
在Laravel的AliasLoader
中,它将像这样注册到spl_autoload_register
:
spl_autoload_register([$this, 'load'], true, true);
spl_autoload_register
对数组 [$this, 'load']
做了什么?
spl_autoload_register
的第一个参数是可调用的,如 documentation 中所述。
callable
类型的 documentation 表示:
A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1. Accessing protected and private methods from within a class is allowed.
对于您的问题,[$this, 'load']
指的是同一 class 上的方法 load()
,其中 spl_autoload_register
被调用。
例如像这样:
class Foo {
public function register() {
spl_autoload_register([$this, 'load'], true, true);
}
public function load($className) {
// do your loading
}
}
$autoloader = new Foo();
$autoloader->register();
在Laravel的AliasLoader
中,它将像这样注册到spl_autoload_register
:
spl_autoload_register([$this, 'load'], true, true);
spl_autoload_register
对数组 [$this, 'load']
做了什么?
spl_autoload_register
的第一个参数是可调用的,如 documentation 中所述。
callable
类型的 documentation 表示:
A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1. Accessing protected and private methods from within a class is allowed.
对于您的问题,[$this, 'load']
指的是同一 class 上的方法 load()
,其中 spl_autoload_register
被调用。
例如像这样:
class Foo {
public function register() {
spl_autoload_register([$this, 'load'], true, true);
}
public function load($className) {
// do your loading
}
}
$autoloader = new Foo();
$autoloader->register();