How to understand spl_autoload_register of PHP API

Something about spl_autoload_register from PHP official website:
bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool$prepend = false ]]] )

Example:
function my_autoloader($class) {
    include 
'classes/' $class '.class.php';
}
spl_autoload_register('my_autoloader');

But how does this work? How does PHP engine know what variable for $class is? 

Here is the answer for my question:
http://stackoverflow.com/questions/11131238/how-to-use-spl-autoload-register

According to Bailey Parker's answerthe function you specify is added to a pool of functions that are responsible for autoloading. Your function is appended to this list (so if there were any in the list already, yours will be last). With this, when you do something like this:
UnloadedClass::someFunc('stuff');
PHP will realize that UnloadedClass hasn't been declared yet. It will then iterate through the SPL autoload function list. It will call each function with one argument: 'UnloadedClass'. Then after each function is called, it checks if the class exists yet. If it doesn't it continues until it reaches the end of the list. If the class is never loaded, you will get a fatal error telling you that the class doesn't exist. 

评论

此博客中的热门博文

Openwrt路由器上配置shadowsocks透明代理+gfwlist(PAC)

Configure shadowsocks transparent proxy + gfwlist(PAC) on OpenWRT Router

Using Haproxy + shadowsocks (ha + ss) to setup multi ss backend and load balance