Function name: spl_autoload_call()
Applicable version: PHP 5 >= 5.1.2, PHP 7
Function Description: The spl_autoload_call() function attempts to call all registered __autoload() functions to load the requested class.
Syntax: bool spl_autoload_call ( string $class_name )
parameter:
Return value:
Notes:
Example:
// 定义一个自动加载函数function my_autoload($class_name) { include $class_name . '.php'; } // 注册自动加载函数spl_autoload_register('my_autoload'); // 尝试加载类$result = spl_autoload_call('MyClass'); if ($result) { echo '类加载成功!'; } else { echo '类加载失败!'; }
In the above example, we first define an automatic loading function named my_autoload()
, which dynamically contains the corresponding PHP file according to the class name. Then, we register the autoload function into the autoload queue through spl_autoload_register()
function.
Next, we call spl_autoload_call()
function to try to load a class named MyClass
. If the class is loaded successfully, the output is "Class loaded successfully!", otherwise the output is "Class loaded failed!".
It should be noted that if no automatic loading function is registered or the corresponding class file is not found, spl_autoload_call()
function will return false.