Php magic method instructions. After PHP5.0, php provides more object-oriented methods to make php more powerful !! Some functions called Magic methods in PHP are introduced here: in fact, in general applications, we all need php, and PHP object-oriented methods to be improved, makes php more powerful !!
Some functions called Magic methods in PHP are introduced here: in fact, they are used in common applications !!
1. _ construct () when instantiating an object, this method of this object is called first.
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 class constructor. if we define the constructor and The _ construct () method at the same time, php5 calls the constructor by default instead of The _ construct () function. Therefore, _ construct () is the default constructor of the class.
2. _ destruct () this method is called when an object or object operation is terminated.
Java code
Class Test {function _ destruct () {echo "end" ;}}$ t = new Test (); the end
Class Test {function _ destruct () {echo "end" ;}}$ t = new Test (); the end
We can release resources at the end of the object operation.
3. _ get () is called when an attempt is made to read an attribute that does not exist.
If you try to read an attribute that does not exist in an object, PHP will give an error message. If the _ get method is added to the class, and we can use this function to implement 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; the output is: name does not exist
Class Test {public function _ get ($ key) {echo $ key. "does not exist" ;}}$ t = new Test (); echo $ t-> name; the output is: name does not exist
4. _ set () is called when you try to write a value to an attribute that does not exist.
Java code
Class Test {public function _ set ($ key, $ value) {echo ''. $ key. "Value ". $ value ;}}$ t = new Test (); $ t-> name = "aninggo"; the output is aninggo.
Class Test {public function _ set ($ key, $ value) {echo ''. $ key. "Value ". $ value ;}}$ t = new Test (); $ t-> name = "aninggo"; the output is aninggo.
5. _ call () this method is called when an object does not exist.
Java code
Class Test {public function _ call ($ Key, $ Args) {echo "the {$ Key} method you want to call does not exist. The parameter you passed in is: ". print_r ($ Args, true) ;}}$ t = new Test (); $ t-> getName (aning, go );
Class Test {public function _ call ($ Key, $ Args) {echo "the {$ Key} method you want to call does not exist. The parameter you passed in is: ". 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 parameter is Array.
(
[0] => aning
[1] => go
)
The getName method you want to call does not exist. The parameter is Array.
(
[0] => aning
[1] => go
)
6. _ toString () is called when an object is printed.
This method is similar to the toString method of java. We call this function when we print the object directly.
Class Test {public function _ toString () {return "print Test" ;}}$ t = new Test (); echo $ t;
When echo $ t; is run, $ t->__ toString () is called to output
Print Test
7. _ clone () is called when the object is cloned.
Class Test {public function _ clone () {echo "I have been copied! ";}}$ T = new Test (); $ t1 = clone $ t; program output: I have been cloned!
SUCCESS !! Some functions called Magic methods in PHP are introduced here: in fact, we all need...