1. Definition
Refers to methods that are automatically called in certain situations
PHP provides a number of magic methods, all starting with __
2. Construction method __construct
This method is called automatically when an object is instantiated
Can be used to instantiate an object Shishun to control the properties of the class
Class Name {
private property name = NULL;
Public function __construct ($ formal parameter) {
$this-Property name = $ formal parameter;
}
}
$ object = new class name (' argument ');
Echo $ object, property name;
3. Destructor Method __destruct
This method is called when the object is destroyed with unset (), another value is assigned to the object, or when the page finishes executing
4. Auto Load __autoload
This method is called automatically when an instantiated class is not loaded into the current file
And the class name is passed in as a parameter
function __autoload ($class) {
Require ('./'. $class. php ');
}
$ object = new class name ();
Attention:
The declaration of a class is globally valid (even inside the function)
You can use Spl_autoload_regsite (' method name ') to customize the method
Register as a method with the "Auto-load" feature
4. Other
Method name Invocation Criteria
__clone () When cloning an object using Clone ()
__get ($ formal parameter) when accessing non-public/undefined properties
__set ($ parameter 1,$ parameter 2) when modifying non-public/Add undefined properties
__call ($ parameter 1,$ parameter 2) when accessing a non-public/undefined method
__callstatic ($ parameter 1,$ parameter 2) when accessing a non-public/undefined static method
__isset () when using isset () for non-public/undefined properties
__unset () when using unset () for non-public/undefined properties
Note: These magic methods can play a very important role in writing the underlying application of the framework.
@zhnoah
Source: http://www.cnblogs.com/zhnoah/
This article is owned by me and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original
The right to pursue legal liability.
PHP Learning Note--10. Magic method