Home > Backend Development > PHP Problem > What are the functions of php magic methods?

What are the functions of php magic methods?

王林
Release: 2023-03-05 20:48:02
Original
4092 people have browsed it

The magic methods and functions of php are: 1. __get, calling undefined attributes; 2. __set, assigning values ​​to undefined attributes; 3. __call, calling undefined methods; 4. __autoload, not defined The class is automatically called; 5. __clone, copy the object.

What are the functions of php magic methods?

The magic methods and functions are as follows:

(Recommended tutorial: php video tutorial)

1. __get and __set

These two methods are designed for properties that are not declared in the class and their parent class

__get( $property ) when calling an undefined property This method is accessed when

__set( $property, $value ) is called when assigning a value to an undefined property

There is no declaration here including properties with access control of protected and private (that is, no permissions) Accessed properties)

2, __isset, __unset

__isset( $property ) This method is called when the isset() function is called on an undefined property

__unset ($property) This method is called when the unset() function is called on an undefined property.

Same as the __get method and __set method. There is no declaration here including attributes whose access control is protected and private. (That is, attributes that do not have permission to access)
3. __call

__call( $method, $arg_array ) This method is called when calling an undefined (including no permission to access) method

4. __autoload

__autoload function is automatically called when using a class that has not yet been defined. This function gives the scripting engine a last chance to load the required classes before PHP fails with an error.

Note: Exceptions thrown in the __autoload function cannot be caught by the catch statement block and result in a fatal error.

5, __construct, __destruct

__construct constructor, this method is called when an object is created. The advantage is that the constructor can have a unique name, regardless of the name of the class in which it is located. What is it, so that when you change the name of the class, you don't need to change the name of the constructor method __destruct destructor method. PHP will call this method before the object is destroyed (that is, before it is cleared from memory)

By default, PHP only releases the memory occupied by the object's properties and destroys the resources associated with the object. Destructors allow you to execute arbitrary code to clear memory after using an object, when PHP determines that your script is no longer related to the object. , the destructor will be called. Within 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 give The variable pointing to the object is assigned any other value, usually by assigning the variable to NULL or calling unset.

6, __clone

Object assignment in PHP5 uses reference assignment. When using the clone method to copy an object, the object will automatically call the __clone magic method. Some initialization operations can be implemented in the __clone method.

7, __toString

__toString method is automatically called when converting an object into a string. For example, when using echo to print an object, if the class does not implement this method, the object cannot be printed through echo. Otherwise it will display: Catchable fatal error: Object of class test could not be converted to string in, this method must return a string.

Before PHP 5.2.0, the __toString method can only take effect when used in conjunction with echo() or print(). After PHP 5.2.0, it can take effect in any string environment (for example, through printf(), using the %s modifier), but cannot be used in non-string environments (such as using the %d modifier). From PHP 5.2.0, if an object that does not define the __toString method is converted to a string, an E_RECOVERABLE_ERROR error will be reported.

8, __sleep, __wakeup

__sleep Use __wakeup when serializing. Call

serialize() during deserialization to check whether there is a magic name in the class_ _sleep function. If so, the function will run before any serialization. It clears the object and should return an array containing the names of all variables in the object that should be serialized.

The purpose of using __sleep is to close any database connections the object may have, submit pending data, or perform similar cleanup tasks. In addition, this function is also useful if you have very large objects that do not need to be stored completely.

In contrast, unserialize() checks for the existence of a function with the magic name __wakeup. This function can reconstruct any resources the object may have, if present. The purpose of using __wakeup is to reestablish any database connections that may have been lost during serialization and to handle other reinitialization tasks.

9, __set_state

When var_export() is called, this static method will be called (valid since PHP 5.1.0). The only parameter of this method is an array containing class properties arranged in the format of array('property' => value, ...).

10. __invoke

When trying to call an object by calling a function, the __invoke method will be automatically called. Valid for PHP5.3.0 or above version
11, __callStatic

It works similar to the __call() magic method. __callStatic() is to handle static method calls. It is valid in PHP5.3.0 and above. PHP has indeed strengthened the definition of the __callStatic() method; it must be public , and must be declared static. Likewise, the __call() magic method must be defined as public, as must all other magic methods.

Related recommendations: php training

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template