Php magic methods

Source: Internet
Author: User
Some functions called Magic methods in PHP are introduced here: in fact, they are used in common applications !! 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, 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.