PHPspl_autoload_register implements automatic loading research. Here we will talk about some features of this function through an experiment. Function prototype: boolspl_autoload_register ([callback $ autoload_function [, bool $ throwtrue [, bool $ prependfalse] here we will talk about some features of this function through an experiment.
Function prototype
Bool spl_autoload_register ([callback $ autoload_function [, bool $ throw = true [, bool $ prepend = false])
Version compatibility
PHP 5> = 5.1.2
Experiment process
Step 1: Use the spl_autoload_register () function to register the load () method.
The code is as follows:
Function load (){
Require_once 'Lib. php ';
}
Spl_autoload_register ('load ');
?>
The lib. php file code is as follows:
The code is as follows:
Class className {
Function method (){
Echo 'a method in class ';
}
}
Function onlyMethod (){
Echo 'method only ';
}
?>
Note: The lib. php file is a className class and an onlyMethod function.
Step 2: Call the automatic loading class
The code is as follows:
$ Class = new className ();
$ Class-> method ();
OnlyMethod ();
Output:
A method in class
Method only
(Instantiate the className class, call the method () function of the class, and call the onlyMethod () method at the same time. the output is normal and no error occurs .)
Step 3: directly call the function
OnlyMethod ();
Note: The onlyMethod () function in the lib. php file is called directly without an instantiation class.
Output:
Fatal error: Call to undefined function onlyMethod () in '... (omitted path )'
Step 4: instantiate the className class and then directly call
$ Class = new className ();
OnlyMethod ();
Output: method only
The above four steps show that if the loaded file contains a function, you must instantiate the class. Otherwise, an exception occurs. Call to undefined function is incorrect, pay attention to the specific usage.
Participant: spl_autoload_register
Bytes. Function prototype: bool spl_autoload_register ([callback $ autoload_function [, bool $ throw = true [, bool $ prepend = false]...