Introduction to the use of the PHP magic method

Source: Internet
Author: User
In object-oriented programming, PHP provides a series of magic methods that provide a lot of convenience for programming. The Magic method in PHP usually starts with (two underscores) and does not require a display of calls but a certain set of conditions. This article briefly summarizes the magic methods available in PHP.

1.construct () when an object is instantiated, this method of the object is called first.

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 same class name function are the constructors of the class, so if we define both the constructor and the construct () method, PHP5 will call the constructor by default instead of calling the construct () function, so construct () As the default constructor for a class
2.destruct () This method is called when an object or object operation is terminated.

Class Test {function destruct () {echo "End"}} $t = new Test () will output end class Test {function destruct () {echo "End"; }} $t = new Test (), the end will be output

We can do things like freeing resources at the end of an object operation.
3.get () is called when attempting to read a property that does not exist.

If you try to read a property that does not exist for an object, PHP will give you an error message. If you add a Get method to a class, and we can use this function to implement a variety of actions like reflection in Java.

Class Test {public function get ($key) {echo $key. "does not exist"; }} $t = new Test (); Echo $t->name; Will output: Name does not exist for class Test {public function get ($key) {echo $key. "does not exist"; }} $t = new Test (); Echo $t->name; It will output: name does not exist

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

Class Test {public function set ($key, $value) {echo ' pair '. $key. "Attached value". $value; }} $t = new Test (); $t->name = "Aninggo"; Will output: The name value Aninggo class Test {public function set ($key, $value) {echo ' pair '. $key. "Attached value". $value; }} $t = new Test (); $t->name = "Aninggo"; Will output: The value of the name attached to the Aninggo

5.call () Call this method when attempting to invoke a method that does not exist for an object.

Class Test {public function call ($Key, $Args) {echo "The {$Key} method you are calling 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 are calling does not exist. The parameter you passed in is: ". Print_r ($Args, true); }} $t = new Test (); $t->getname (Aning,go);

The program will output:
The GetName method you are calling does not exist. parameter is: Array
(
[0] = aning
[1] = = Go
)
The GetName method you are calling does not exist. parameter is: Array
(
[0] = aning
[1] = = Go
)
6.toString () Called when an object is printed

This method is similar to the ToString method of Java, when we directly print the object callback with this function
Class Test {public Function toString () {return ' Print Test ';}} $t = new test (); Echo $t;
When the Echo $t is run, $t->tostring () is called and the output
Print Test
7.clone () is called when the object is cloned
Class Test {public Function clone () {echo "I was copied! "; } } $t = new Test (); $t 1 = clone $t; program output: I was 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.