Home  >  Article  >  Backend Development  >  HidRegisterMinidriver PHP spl_autoload_register implements automatic loading research

HidRegisterMinidriver PHP spl_autoload_register implements automatic loading research

WBOY
WBOYOriginal
2016-07-29 08:47:251142browse

Here we talk about some characteristics of this function through an experiment.
Function prototype
bool spl_autoload_register ([ callback $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
Version compatible
PHP 5 >= 5.1.2
Experimental process
Step one, use The spl_autoload_register() function registers the load() method

Copy the code The code is as follows:


function load(){
require_once 'lib.php';
}
spl_autoload_register('load') ;
?>


The lib.php file code is as follows

Copy the code The code is as follows:


class className{
function method(){
echo 'a method in class
Class



Copy code

The code is as follows:



$class = new className();
$class->method();

onlyMethod(); Output: a method in class

method only

Explanation: Instantiate the className class, call the class method() function, and call the onlyMethod() method at the same time. The output is normal and no errors occur.
The third step, directly call the function
onlyMethod();
Explanation: There is no instantiated class , directly call the onlyMethod() function in the lib.php file
Output:

Fatal error: Call to undefined function onlyMethod() in '...(omit path)'
The fourth step, instantiate the className class, and then call it directly
$class = new className();
onlyMethod();
Output: method only
From the above four-step experiment, we found that if the loaded file contains a function, the class inside must be instantiated, otherwise an exception will occur. Call to undefined function error, please pay attention to it when using it.

Participation information: spl_autoload_register

The above introduces the research on automatic loading of HidRegisterMinidriver PHP spl_autoload_register, including the content of HidRegisterMinidriver. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn