Home  >  Article  >  Backend Development  >  What are the magic-like methods in php

What are the magic-like methods in php

王林
王林Original
2020-08-20 13:50:172341browse

The class magic methods in php are: 1. __sleep, which returns an array containing the names of all variables in the object that should be serialized; 2. __tostring, which returns a class to be output when it is treated as a string. Content; 3. __construct, construction method; 4. __destruct, destruction method.

What are the magic-like methods in php

Keep all class methods starting with a double underscore (__) as magic methods in PHP.

(Recommended tutorial: php graphic tutorial)

Let’s take a look at the magic methods:

1, __sleep: Return a An array containing the names of all variables in the object that should be serialized.

The serialize function first checks whether the __sleep method exists in the class when serializing the class. If present, this method will be called before the serialization operation is performed, and only the properties in the array returned by __sleep will be serialized. If __sleep returns nothing, null will be serialized and an E_NOTICE level error will be generated.

__sleep cannot return private members of the parent class, otherwise an E_NOTICE level error will occur. This method is useful for objects that are large but don't need to hold all the data.

2. __wakeup: Contrary to __sleep, when the unserialize function is deserializing, it will first check whether the __wakeup method exists in the class. If it exists, the method will be called first and then the deserialization operation will be performed. Used to prepare resources required by some objects before deserialization, or other initialization operations.

3. __tostring: Returns the content to be output when a class is treated as a string. This method must return a string and cannot throw an exception in this method, otherwise a fatal error will occur.

Before PHP5.2, this method only took effect when using echo and print directly. After that, it can take effect in any string environment. If an object that does not define the __toString method is converted to a string, it will A fatal error occurs.

(Video tutorial recommendation: php video tutorial)

4. __invoke: Starting from PHP5.3, this method will be called when trying to call an object as a function. .

5. __set_state: Starting from PHP5.1, this static method will be called when the var_export function is called to export a class. This method has only one parameter, which is an array containing attributes as keys and attribute values ​​as values. This method can be used to control which members can be exported.

6. __debugInfo: Starting from PHP 5.6, this method will be called when the var_dump function is called to print the attributes of an object. This method can be used to control which attributes can be printed. If this method is not defined, the All public, protected, and private properties will be printed. Returns an array containing properties that can be printed.

7. __construct: constructor.

8. __destruct: destructor.

9, __set, __get, __isset, __unset: overloaded attributes.

10, __call, __callstatic: overloaded method.

11. __clone: ​​Object copy.

The above is the detailed content of What are the magic-like methods in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn