Home >Backend Development >PHP Problem >What does php magic method do?
The functions of php magic methods: 1. "__construct()" is automatically called when instantiating the object; 2. "__destruct()" is automatically called when the object is destroyed or the script execution ends; 3. "__call()" Automatically called when calling a method that does not exist on the object; 4. "__get()" is automatically called when obtaining attributes that do not exist in the object; 5. "__set()" is automatically called when setting attributes that do not exist in the object; 6. "__isset()" "Automatically called when detecting whether a certain attribute of the object exists, etc.
The operating environment of this tutorial: Windows 7 system, PHP 8 version, DELL G3 computer.
The role of php magic methods:
1, __construct()
: Automatically called when instantiating an object.
2, __destruct()
: Automatically called when the object is destroyed or the script execution ends.
3, __call()
: This function is executed when calling a method that does not exist on the object.
4, __get()
: Execute this function when obtaining non-existing attributes of the object.
5, __set()
: Execute this function when setting attributes that do not exist in the object.
6, __isset()
: This function is executed when detecting whether an attribute of the object exists.
7, __unset()
: Execute this function when destroying an attribute of the object.
8, __toString()
: Execute this function when outputting the object as a string.
9, __clone()
: Execute this function when cloning an object.
10, __autoload()
: When instantiating an object, when the class does not exist, execute this function to automatically load the class.
11, __sleep()
: serialize is called before, you can specify the object attributes to be serialized.
12, __wakeup
: Unserialize is called before and can perform object initialization.
13, __set_state()
: Called when var_export is called. Use the return value of __set_state as the return value of var_export.
14, __invoke()
: This method is executed when using the object as a function. This is generally not recommended.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does php magic method do?. For more information, please follow other related articles on the PHP Chinese website!