Summary of magic methods for php surface objects

Source: Internet
Author: User
Tags autoload
What are PHP object-oriented magic methods and how each magic method works? please write a Test class and run
 Non-empty string, non-zero, true, array with units will be converted to true; 0, "0", empty * string, NULL, an array without cells will be converted to false). * parameter: $ name indicates the name of the member variable to be detected. the passing of this parameter is automatically completed by PHP. * return value: type (uncertain) note that this method can be set to private (the constructor should not be set to private). when isset () is used () the function can directly detect * public member variables. if The _ isset () method is not used, direct detection of private members will result in inaccurate results, that is, isset () * always returns false; * _ unset ($ name) * function: This method is automatically called When unset ($ name) is used to cancel a private member variable; * parameter: $ name indicates the name of the member variable to be revoked. this parameter is automatically passed by PHP. * return value: the value cannot be returned. an error occurs when the value is forcibly returned, when unset () function is used to revoke a Public * Member variable, it can be directly revoked. if The _ unset () method is not used * _ ToString () * function: PHP automatically calls this method when trying to output an object type variable to a certain extent. * parameter: no parameter, if an error is reported, * return value: string (this method must return a string); * _ clone () * function: PHP automatically calls this method when cloning an object with the clone keyword to add its own properties and methods to the cloned object. * parameter: no parameter is required; * return value: no return value. * _ call ($ name, $ argument) * function: This method is automatically called when a program attempts to call a nonexistent member method. * parameter: PHP will automatically pass the called non-existing method name to $ name, and the parameters in the non-existing method will be integrated into an array and passed to * $ argument; * return value: no return value; * _ sleep (): when using the serialize () function, if the instantiated object contains the _ sleep () method, the _ sleep () method is executed first. You can clear the * object and return an array of all the variables in the object. The _ sleep () method is used to disable similar * aftercare * parameters such as database connections that an object may have: no parameters need to be passed * return value: Array type (Array) * _ wakeup () * function of all variables in this object: Use the unserialize () function to restore an object serialized by the serialize () function, __wakeup () the method is to recover * lost database connections and related work * parameters in serialization: no parameter needs to be passed * return value: ------------------------ * _ set_state ($ arr) * function: you can use var_export () to obtain the static method _ set_state () of the handler class. this method must be static and must return an object * parameter: $ arr as an array * returned value: as an object ** _ autoload ($ class_name) * function: automatically searches for and imports the class required for instantiation in the specified path * parameter: $ class_name indicates the class name * returned value: * _ call Static ($ name, $ argument) * function: This method is automatically called when a program attempts to call an undefined or invisible member method in a Static method. * parameter: PHP will automatically pass the called non-existing method name to $ name, and the parameters in the non-existing method will be integrated into an array and passed to $ argument; * return value: no return value; * Note: PHP5.3 and later versions support this magic method * Note: __classstatic () must be declared as public and static; otherwise, a warning error * _ invoke ($ x, $ y ,...) * function: when an object is called by calling a function, the __invoke method is automatically called. * Parameter: any number of parameters passed when an object is called using a function call method * return value: uncertain * Note: PHP5.3 and later versions support this magic method **/class XuDianYang {public $ sex; public $ age; private $ girlfriend = 'XX'; public function _ construct ($ sex, $ age) {$ this-> sex = $ sex; $ this-> age = $ age;} public function _ get ($ name) {echo _ class __. "::". $ name. "nonexistent or invisible";} public function _ set ($ name, $ value) {echo _ class __. "::". $ name. "Assignment failed because it is not present or invisible";} public function _ isset ($ name) {if (isset ($ this-> $ name) {echo 1 ;} else {echo 0 ;}} public function _ unset ($ name) {unset ($ this-> $ name);} public function _ toString () {return _ class __;} public function _ clone () {$ this-> sex = 'femal';} public function _ call ($ name, $ argument) {echo "method ". _ class __. "::". $ name. "does not exist";} public function _ sleep () {echo "I'm sleeping"; foreach ($ this as $ key => $ value) {$ arr [] = $ key;} return $ arr;} public function _ wakeup () {echo "I wake up";} public function _ set_state ($ arr) {$ obj = new CheShi (); $ obj-> var1 = $ arr ['sex']; $ obj-> var2 = $ arr ['age']; return $ obj ;}} function _ autoload ($ name) {$ class_path = ". /"; include_once ($ class_path. $ name. ". class. php ") ;}$ xudianyang = new XuDianYang (" male ", 21); echo"
"; // Normal access to sex properties echo $ xudianyang-> sex; echo"
"; // Because the weight attribute does not exist, the _ get () method echo $ xudianyang-> weight; echo" is automatically called"
"; // Because it is invisible (private), The _ get () method $ xudianyang-> girlfriend; echo" is automatically called"
"; // The value assignment fails because it does not exist. The _ set () method $ xudianyang-> shengao =" 172 "; echo" is automatically called"
"; // Check whether the sex property exists. because sex public directly detects var_dump (isset ($ xudianyang-> sex); echo"
"; // Checks whether girlfriend exists. because girlfriend is private, the _ isset () method isset ($ xudianyang-> girlfriend) is automatically called. echo"
"; // Undo girlfriend. because girlfriend is private, the _ unset () method is automatically called. // unset ($ xudianyang-> girlfriend); isset ($ xudianyang-> girlfriend ); echo"
"; // Directly output an object and automatically call the _ toString () method echo $ xudianyang; echo"
"; $ Xu = clone $ xudianyang; echo $ xu-> sex; echo"
"; // Because the call does not exist, the _ call () method $ xudianyang-> walk (); echo" is automatically called"
"; // Serialize the object, automatically call the _ sleep () method $ a = serialize ($ xudianyang); echo"
"; Echo" the serialization result is: ". $ a; // deserialization object, automatically calling the _ wakeup () method echo"
"; $ B = unserialize ($ a); // deserialization result echo $ B-> sex; echo"
"; Eval ('$ c ='. var_export ($ xudianyang, true)."; "); echo $ c-> var1;?> CheShi. class. php

 

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.