PHP Magic method using instructions _php tips

Source: Internet
Author: User
Tags object model
After PHP5.0, PHP object-oriented commission more methods, making PHP more powerful!!
Some of the functions in PHP called Magic Method, introduced here: in fact, in the general application, we all need to use them!!

1.__construct () When instantiating an object, this method of this object is invoked 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 same class name function are constructors of the class, so if we both define constructors and __construct () methods, PHP5 calls the constructor by default and does not invoke the __construct () function, so __construct () As the default constructor for a class
2.__destruct () This method is invoked when an object or object operation is terminated.
Java code
Class Test {function __destruct () {echo ' End ';}} $t = new Test ();
Class Test {function __destruct () {echo ' End ';}} $t = new Test ();
We can do things like releasing resources at the end of an object operation.
3.__get () is invoked when an attempt is made to read an attribute 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 various operations like 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
4.__set () is invoked when an attempt is made to write a value to a property that does not exist.
Java code
Class Test {public Function __set ($key, $value) {echo ' to '. $key. "Attached value". $value; }} $t = new Test (); $t->name = "Aninggo"; Is output: The name attached to the value Aninggo
Class Test {public Function __set ($key, $value) {echo ' to '. $key. "Attached value". $value; }} $t = new Test (); $t->name = "Aninggo"; Is output: The name attached to the value Aninggo

5.__call () This method is invoked when an attempt is made to invoke a method that does not exist for an object.
Java code
Class Test {public Function __call ($Key, $Args) {echo] the {$Key} method you are calling does not exist. The parameters you pass in are: ". 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 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 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 () is called when an object is printed

This method is similar to the Java ToString method, and when we print the object directly, the callback uses this function
Class Test {public Function __tostring () {return "Print Test";}} $t = new test (); Echo $t;
When the Echo $t is run, the $t->__tostring () is invoked, which outputs
Print Test
7.__clone () 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.