Magic Method:
PHP treats all the methods in the class that begin with __ (two underscores) as a magic method, and it is generally recommended that users do not prefix the custom method with __. Magic Method:
1. __construct ()
The default constructor method for a class, if __construct () and a method with the same name as the class, is called by default __construct () instead of the same name method. In general, the user-defined construction method will also use __construct ().
2. __destruct ()
A destructor for a class that executes when all references to the object are deleted, or when an object is explicitly destroyed.
3. __get ($name)
Can be summed up as: When reading an object's properties in a $object->a way, if property a exists and is public, the value of the property is returned directly, and if the property A does not exist or is a type that is not directly accessible by protected/private, it will be called __ The Get ($name) method, whichever is the return value. You can generally use this method to allow externally restricted access to internal properties, or to perform reflection operations similar to those in Java.
4. __set ($name, $value)
Similar to __get ($name), when assigning a value to a property in the form of $object->a = 17, if property a exists and is public, then the property A is assigned directly to it, and if the property A does not exist or is a protected/private type, it will be called __ Set ($name, $value) method.
5. __call ($name, $arguments)/ __callstatic ($name, $arguments)
The __call ($name, $arguments) method is called when a method that does not exist or is not accessible is called. The __callstatic ($name, $arguments) method is called when a non-existent or inaccessible method is called in a static method.
6. __tostring ()
It is called directly when the object is printed. such as Echo $object;
7. __clone ()
Called directly when the object is copied. such as $ A = new Action (); $a = $object;
8. __isset ($name)/__unset ($name)
When you use Isset () or empty () for a property that does not exist or is not accessible, __isset () is called, and __unset () is called when unset a nonexistent or inaccessible property, otherwise the attribute is directly unset.
9. __set_state ()
When an object is output with Var_export (), __set_state () is called, and the output is based on the return value of the Magic method. Note: Var_export () is similar to Var_dump (), except that the contents of the Var_export () output conform to the PHP syntax.Note how to use:
$test = new test ();
$b = Var_export ($test, true);
Var_dump ($b);
Class Test {
public $a;
public static function __set_state ($array) {
$ab = new Test ();
$ab->a = 10;
return $ab;
}
}
__autoload ($name)
When an object is instantiated, the method is called if the corresponding class does not exist. Note: The method is a global function, and the parameter is the name of the class.
__sleep ()/__wakup ()
Slightly.
Magic constants:
1. __line__
Returns the current line number in the file.
2. __file__
Returns the full path of the file in which it resides.
3. __function__
Returns the name of the function being located.
4. __class__
Returns the name of the class in which it is located.
5. __method__
Returns the name of the class method in which it is located. Note that __method__ returns the form of "class::function", while __function__ returns the form of "function".
PHP--Magic method, magic constant