Home >Backend Development >PHP7 >What are the magic methods in php7

What are the magic methods in php7

藏色散人
藏色散人Original
2021-12-10 09:33:335249browse

php7 magic methods include: 1. "__contract" method; 2. "__destruct" method; 3. "__set" method; 4. "__get" method; 5. "__call" method; 6. "__callStatic" "method; 7. "__toString" method and so on.

What are the magic methods in php7

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

What are the magic methods in php7?

A magic method is a special method that overrides PHP's default actions when performing certain operations on an object.

PHP reserves all method names starting with __. Therefore, the use of such method names is not recommended unless overriding PHP's behavior.

php7 magic methods are:

__contract: Triggered when a class is instantiated

__destruct: Triggered when an instance object is destroyed

__set(string $name, mixed $value): Triggered when setting an inaccessible member attribute

__get(string $name): Triggered when getting an inaccessible member attribute

__call($name,$arguments): Triggered when accessing an inaccessible member method

__callStatic($name,$arguments): Triggered when accessing an inaccessible member static method

__toString: Triggered when an object instance is treated as a string

__clone: ​​Triggered when creating a new object

__invoke: Triggered when the object is called as a function

__sleep: Triggered when data processing of an object does not require saving of all data, such as: serialize() serialization

__wakeup: When unserialize(), it will first check whether there is __wakeup Method

__unset(string $name): Triggered when an inaccessible object property is unset

__isset(string $name): Triggered when an inaccessible object property is isset, empty Trigger

__debugInfo: Triggered when an object is var_dump

__set_state: Triggered when an object is var_export

Recommended learning: "PHP Video Tutorial

The above is the detailed content of What are the magic methods in php7. 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