PHP comprehension 3: PHP classes automatically load [convert] to: www.liuhui.infoarticle78.html? The spl_autoload_register () function should be one of the most used and very core functions in mainstream frameworks. it can automatically register functions and classes and implement functions similar to the _ autoload () function, this simplifies the call and loading of classes and improves the efficiency of work. here we will talk about this PHP comprehension through an experiment: automatically loading PHP classes]
Turn: http://www.liuhui.info/article78.html
?
The spl_autoload_register () function should be one of the most used and very core functions in mainstream frameworks. it can automatically register functions and classes and implement functions similar to the _ autoload () function, it simplifies class calling and loading and improves work efficiency. here we will talk about some features of this function through an experiment.
Function prototype
BoolSpl_autoload_register? ([? Callback $ autoload_function? [,? Bool $ throw = true? [,? Bool $ prepend = false? ])
Version compatibility
PHP 5> = 5.1.2
?
Step 1: Use the spl_autoload_register () function to register the load () method.
? The lib. php file code is as follows:
? Note: The lib. php file is a className class and an onlyMethod function.
? Note: class definitions must be included in the PHP file loaded automatically.
?
Step 2: Call the automatic loading class
$class = new className();$class->method();onlyMethod();
? Output: a method in class
???????????? Method only
?
Step 3: directly call the function
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.
?
?
?
?
?
?