PHP stipulates that methods starting with two underscores (__) are retained as magic methods, so it is recommended that you do not start with __ unless it is to overload existing magic methods.
The magic methods in PHP are: __construct, __destruct, __call, __callStatic, __get, __set, __isset, __unset, __sleep, __wakeup, __toString, __set_state, __clone, __autoload
1、__get、__set
These two methods are designed for properties that are not declared in the class and their parent class.
__get( $property ) When an undefined property is called, this method will be triggered, and the passed parameter is the property name that is accessed.
__set( $property, $value) When assigning a value to an undefined property, this method will be triggered. The passed parameters are the property name and value that are set.
The non-declaration here includes properties that are protected, private when invoked using an object (i.e. properties that do not have permission to access).
2、__isset、__unset
__isset( $property ) Call this method when the isset() function is called on an undefined property
__unset( $property ) Call this method when the unset() function is called on an undefined property
The same as the __get method and the __set method, the no declaration here includes the properties that are protected and private when called using the object (i.e., properties that do not have permission to access)
3、__call
__call( $method, $arg_array ) When calling an undefined method, this method is called
The undefined methods here include methods that do not have permission to access; if the method does not exist, go to the parent class to find this method; if the parent class does not exist, call the __call() method of this class; if the __call() method does not exist in this class, go to the __call() method of this class.
4、__autoload
__autoload function, which is automatically called when trying to use a class that has not been defined yet. By calling this function, the script engine has the last chance to load the required class before the PHP error fails.
If you want to define a global autoload class, you must use the spl_autoload_register() method to register the processing class to the PHP standard library:
<?php
class Loader
{
static function autoload_class($class_name)
{
//Look for the correct $class_name class and introduce it, if no exception is thrown
}
}
/**
* Set the automatic loading of objects
* spl_autoload_register — Register given function as __autoload() implementation
*/
spl_autoload_register(array('Loader', 'autoload_class'));
$a = new Test();//Test is instantiated without requiring, realizing automatic loading. Many frameworks use this method to automatically load classes
?>
Note: Exceptions thrown in the __autoload function cannot be caught by the catch statement block and cause a fatal error, so it should be captured in the function itself.
5、__construct、__destruct
__construct constructor. When an object is created, the advantage of using this method compared to PHP4 is that it can make the constructor have a unique name, regardless of the name of the class it is located. In this way, when you change the name of the class, you do not need to change the name of the constructor.
__destruct destruct method, which PHP will call before the object is destroyed (i.e. before it is cleared from memory). By default, PHP only releases the memory occupied by object properties and destroys object-related resources. The destructor allows you to execute arbitrary code after using an object to clear memory. When PHP decides that your script is no longer related to the object, the destructor will be called.
In a function's namespace, this happens when the function returns.
For global variables, this happens at the end of the script.
If you want to explicitly destroy an object, you can assign any other value to the variable pointing to the object. Usually, the variable is assigned to NULL or call unset.
6、__clone
Object assignment in PHP5 is a reference assignment used. If you want to copy an object, you need to use the clone method. When calling this method, the object will automatically call the __clone magic method. If some initialization operations are required during object copying, it can be implemented in the __clone method.
7、__toString
The __toString method is automatically called when converting an object into a string, such as when printing an object using echo.
If the class does not implement this method, the object cannot be printed by echo, otherwise it will be displayed: Catchable fatal error: Object of class test could not be converted to string in
This method must return a string.
Prior to PHP 5.2.0, the __toString method could only take effect when using echo() or print() in combination. After PHP 5.2.0, it can take effect in any string environment (for example through printf(), using the %s modifier), but it cannot be used in non-string environments (for example using the %d modifier). From PHP 5.2.0, if an object with an undefined __toString method is converted to a string, an E_RECOVERABLE_ERROR error will be reported.
8、__sleep、__wakeup
__sleep is used when serializing
__wakeup is called when it is reverse serialized
serialize() Checks whether there is a function with the magic name __sleep in the class. If so, the function will run before any serialization. It can clear the object and should return an array containing all the variable names in the object that should be serialized.
The purpose of using __sleep is to close any database connections that an object may have, submit waiting data, or perform similar cleanup tasks. In addition, this function is also useful if there are very large objects that do not need to be stored completely.
Instead, unserialize() checks the existence of a function with the magic name __wakeup. If present, this function can rebuild any resources the object may have.
The purpose of using __wakeup is to rebuild any database connections that may be lost in serialization and to handle other reinitialization tasks.
9、__set_state
When var_export() is called, this static method will be called (effective since PHP 5.1.0).
The only parameter of this method is an array containing class properties arranged in array('property' => value, …).
10、__invoke
When trying to call an object in the form of a calling function, the __invoke method is automatically called.
PHP5.3.0 or above is valid
11、__callStatic
It works similar to the magic method of __callStatic(), which is to handle static method calls,
PHP5.3.0 or above is valid
PHP does strengthen the definition of the __callStatic() method; it must be public and must be declared static. Similarly, the __call() magic method must be defined as public, and all other magic methods must be so