Home > Backend Development > PHP Tutorial > Examples of using php magic methods

Examples of using php magic methods

怪我咯
Release: 2023-03-12 16:58:01
Original
1232 people have browsed it

Some functions called Magic methods in PHP are introduced here: In fact, in general applications, we all need to use them! !

After PHP5.0, php object-oriented commissions more methods, making php more powerful! !
Some functions called magic methods in PHP are introduced here: In fact, in general applications, we all need to use them! !

1.construct() When instantiating an object, this method of the object is first called.
Java code
class Test { function construct() { echo "before"; } } $t = new Test();
class Test { function construct() { echo "before"; } } $t = new Test();
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.
Java code
class Test { function destruct() { echo "end"; } } $t = new Test(); will output end
class Test { function destruct() { echo "end"; } } $t = new Test(); will output end
We can then perform operations such as releasing resources at the end of the object operation
3.get() When trying Called when reading a property that does not exist.

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.
Java code
class Test { public function get($key) { echo $key . " does not exist"; } } $t = new Test(); echo $t->name; will output: name does not exist
class Test { public function get($key) { echo $key . "Does not exist"; } } $t = new Test(); echo $t->name; will output: name does not exist Exists
4.set() is called when trying to write a value to a property that does not exist.
Java code
class Test { public function set($key,$value) { echo 'pair'.$key . "attached value".$value; } } $t = new Test() ; $t->name = "aninggo"; will output: value attached to name aninggo
class Test { public function set($key,$value) { echo 'pair'.$key . "value attached" .$value; } } $t = new Test(); $t->name = "aninggo"; will output: append value to name aninggo

5.call() when trying This method is called when calling a method on an object that does not exist.
Java code
class Test { public function call($Key, $Args) { echo "The {$Key} method you want to call does not exist. The parameters you passed in are:" . print_r( $Args, true); } } $t = new Test(); $t->getName(aning,go);
class Test { public function call($Key, $Args) { echo "You want to call The {$Key} method does not exist. The parameters you pass in are: " . print_r($Args, true); } } $t = new Test(); $t->getName(aning,go);
The program will output:
Java code
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 Examples of using 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