Home > Backend Development > PHP Tutorial > PHP magic methods Magic methods in PHP classes

PHP magic methods Magic methods in PHP classes

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-28 08:25:33
Original
933 people have browsed it

Definition:

Methods starting with two underscores "__" in a PHP class are called magic methods.

Category:

For example: Construction method: __construct; Destruction method: __destruct; Dynamic overloading: __set(), __get(), __call(), __callStatic(); Object cloning: __clone()

Application scenarios :

1. When the program attempts to modify a non-existent or invisible class attribute, the PHP engine will call the __set() method, of course, provided that the method is defined in the class. The definition format of __set() is as follows:

function __set($name,$value){

                                                                                                                                                                                                  .

2. When the program tries to read a non-existent or invisible class attribute, the PHP engine will call the __get() method, of course, provided that the method is defined in the class. The definition format of __get() is as follows:

function __get($name){

            // Implementation details

    }                                                                             ’

3. When the program tries to call a non-existent or invisible class method, the PHP engine will call the __call() method, of course, provided that the method is defined in the class. The definition format of the __call() method is as follows:

function __call($name,$args){

, exists in the form of an array.

4. Starting from PHP5.3.0, you can use __callStatic() to dynamically create static methods. The definition format of __callStatic() is as follows:

Function __callStatic($name,$args){

                                                                    to ’s ’s __callStatic’s __callStatic's __callStatic's __callStatic('s-to-know-to-result to __callStatic() to function __callStatic($name,$args) {

Exists in the form of an array.

5. Once the __clone() method is defined, the class will automatically call it when it is copied, so that we can re-open memory for the reference attributes in the __clone() method. The definition format of __clone() is as follows:

function __clone(){

//Implementation details

}

The above has introduced the PHP magic methods in the PHP class, including the content of PHP magic methods. I hope it will be helpful to friends who are interested in PHP tutorials.

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