Home > Backend Development > PHP Tutorial > Introduction to using php magic method

Introduction to using php magic method

怪我咯
Release: 2023-03-13 22:28:01
Original
1314 people have browsed it

In Object-orientedProgramming, PHP provides a series of magic methods, which provide a lot of convenience for programming. Magic methods in PHP usually start with (two underscores) and do not require explicit calls but are triggered by certain conditions. This article briefly summarizes the magic methods available in PHP.

#1.construct() When instantiating an object, this method of the object is first called.

class Test { function construct() { echo "before"; } } $t = new Test(); 
class Test { function construct() { echo "before"; } } $t = new Test();
Copy after login

The output is:
start
We know that the php5 object model and the function with the same class name are the constructor of the class, then if we simultaneously If you define a constructor and construct() method, php5 will call the constructor by default instead of the construct() function, so construct() serves as the default constructor of the class
2.destruct() When deleting a This method is called when the object or object operation terminates.

class Test { function destruct() { echo "end"; } } $t = new Test();将会输出end 
class Test { function destruct() { echo "end"; } } $t = new Test();将会输出end
Copy after login

We can perform operations such as releasing resources at the end of the object operation
3.get() When trying to read a property that does not exist when called.

If you try to read a property that does not exist in an object, PHP will give an error message. If we add a get method to the class, we can use this function to implement various operations similar to reflection in Java.

class Test { public function get($key) { echo $key . " 不存在"; } } $t = new Test(); echo $t->name; 就会输出:name 不存在 
class Test { public function get($key) { echo $key . " 不存在"; } } $t = new Test(); echo $t->name; 就会输出:name 不存在
Copy after login

4.set() is called when trying to write a value to a property that does not exist.

class Test { public function set($key,$value) { echo '对'.$key . "附值".$value; } } $t = new Test(); $t->name = "aninggo"; 就会输出:对 name 附值 aninggo 
class Test { public function set($key,$value) { echo '对'.$key . "附值".$value; } } $t = new Test(); $t->name = "aninggo"; 就会输出:对 name 附值 aninggo
Copy after login

5.call() This method is called when trying to call a method that does not exist on the object.

class Test { public function call($Key, $Args) { echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true); } } $t = new Test(); $t->getName(aning,go); 
class Test { public function call($Key, $Args) { echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true); } } $t = new Test(); $t->getName(aning,go);
Copy after login

The program will output:
The getName method you want to call does not exist. The parameters are: Array
(
[0] => aning
[1] => go
)
The getName method you want to call does not exist. The parameters are: Array
(
[0] => aning
[1] => go
)
6.toString() is used when printing an object Call

This method is similar to java's toString method. When we print the object directly, we call this function
class Test { public function toString() { return " Print Test"; } } $t = new Test(); echo $t;
When echo $t; is run, $t->toString(); will be called to output
Print Test
7.clone() is called when the object is cloned
class Test { public function clone() { echo "I was copied!"; } }$t = new Test() ; $t1 = clone $t;Program output: I have been cloned!

The above is the detailed content of Introduction to using php magic method. 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