Chinese description and examples of magic methods for php surface objects

Source: Internet
Author: User
Tags autoload constructor sleep

Set meaning

Methods starting with two underscores in PHP, __construct (), _ destruct (), _ call (), _ callStatic (), __get (), _ set (), _ isset (), _ unset (), _ sleep (), _ wakeup (), _ toString (), _ set_state, () _ clone () _ autoload () and so on are called "Magic methods" (Magic methods ). If you want PHP to call these magic methods, you must first define them in the class. Otherwise, PHP will not execute the uncreated magic methods. [1]

Note: PHP retains all class methods starting with _ (two underscores) as magic methods. Therefore, we recommend that you do not use the _ prefix in addition to the above magic methods when defining class methods.


Magic methods

* What are PHP object-oriented magic methods and how each magic method works? Write a Test class and run it.
*
* _ Construct ($ argv1, $ argv2 ,...)
* Function: class constructor, which is used to initialize objects.
* Parameter: any number or type
* Return value: None
* _ Destruct ()
* Function: class destructor called when an object is destroyed. The function is to release the memory.
* Parameter: None
* Return value: None
* _ Get ($ name)
* Function: This method is automatically called when the program attempts to call an undefined or invisible member variable, at this time, the return value of the member variable that the program tries to call * is the return value of this method;
* Parameter: $ name indicates the name of the member variable to be called. This parameter is automatically passed by PHP;
* Return value: type (uncertain). Note that this method can be set to private (the constructor should not be set to private ).
* _ Set ($ name, $ value) magic method
* Function: This method is automatically called when a program attempts to assign values to an undefined or invisible member variable;
* Parameter: $ name and $ value indicate the name of the member variable to be assigned and the value of the member variable respectively. The transfer of these two parameters is automatically completed by PHP;
* Return value: This method does not return values. Note that this method can be set to private (the constructor cannot be set to private ).
* _ Isset ($ name)
* Function: This method is automatically called when an isset ($ name) is used to detect a private member variable. The returned value of isset ($ name) is
* Boolean value converted for the return value of this method (<except "0"> non-null string, non-zero, true, an array with units will be converted to true; 0, "0", NULL * string, NULL, array without cells will be converted to false );
* Parameter: $ name indicates the name of the member variable to be detected. This parameter is automatically transferred by PHP;
* Return value: type (uncertain). Note that this method can be set to private (the constructor should not be set to private). If the isset () function is used to detect
* Public member variables can be directly detected. If The _ isset () method is not used to directly detect private members, an inaccurate result is returned, 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. The parameter is automatically transferred by PHP;
* Return value: the value cannot be returned. An error occurs when the unset () function is used to cancel a public request.
* Member variables can be directly revoked. If The _ unset () method is not used, directly revoking a private member will not succeed;
* _ ToString ()
* Function: PHP automatically calls this method when trying to output an object type variable to a certain extent;
* Parameter: no parameter. If yes, an error is prompted;
* Return value: struct (this method must return a string through return );
* _ Clone ()
* Function: PHP automatically calls this method when cloning an object with the clone keyword to add its own owner to the cloned object.
* Features and methods;
* Parameter: no parameter is required;
* Return value: no return value is required;
* _ Call ($ name, $ argument)
* Function: This method is automatically called when a program attempts to call a nonexistent member method;
* Parameter: PHP automatically passes the called non-existing method name to $ name, and the parameters in the non-existing method are integrated into an array and passed to * $ argument;
* Return value: no return value is required;
* _ Sleep ()
Function: when using the serialize () function, if the instantiated object contains the _ sleep () method, the _ sleep () method is executed first, which can be cleared.
* Returns an array of all the variables in the object. The purpose of using the _ sleep () method is to close the database connection that an object may have.
* Aftercare
* Parameter: no parameter is required.
* Return value: Array of all variables in the object
* _ Wakeup ()
* Function: Use the unserialize () function to re-restore an object serialized by the serialize () function. The __wakeup () method is possible to restore the serialization.
* Lost database connections and related work
* Parameter: no parameter is required.
* Return value :----------------------------
* _ Set_state ($ arr)
* Function: you can use var_export () to initialize the static method _ set_state () of the class. This method must be static and must return an object.
* Parameter: $ arr is an array.
* Return value: an object.
*
* _ Autoload ($ class_name)
* Function: automatically searches for and imports the classes required for instantiation in the specified path.
* Parameter: $ class_name indicates the class name.
* Return value: * _ callStatic ($ name, $ argument)
* Function: This method is automatically called when a program tries to call an undefined or invisible member method in a static method;
* Parameter: PHP automatically passes the called non-existing method name to $ name, and the parameters in the non-existing method are integrated into an array and passed to $ argument;
* Return value: no return value is required. * Note: PHP5.3 and later versions support this magic method.
* Note: __ the classStatic () method must be declared public and static. Otherwise, a warning error will occur.
* _ 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: Not sure * Note: PHP5.3 and later versions support this magic method.
**/


Magic example

 

The code is as follows: Copy code

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. "does not exist or is invisible ";
 }
Public function _ set ($ name, $ value ){
Echo _ class _. ":". $ name. "The assignment fails 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 = 'female ';
 }
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 "<br/> ";
// Access the sex attribute normally
Echo $ xudianyang-> sex;
Echo "<br/> ";
// Because the weight attribute does not exist, the _ get () method is automatically called.
Echo $ xudianyang-> weight;
Echo "<br/> ";
// Because it is invisible (private), The _ get () method is automatically called.
$ Xudianyang-> girlfriend;
Echo "<br/> ";
// The value assignment fails because it does not exist. The _ set () method is automatically called.
$ Xudianyang-> shengao = "172 ";
Echo "<br/> ";
// Check whether the sex property exists.
Var_dump (isset ($ xudianyang-> sex ));
Echo "<br/> ";
// Checks whether girlfriend exists. Because girlfriend is private, the _ isset () method is automatically called.
Isset ($ xudianyang-> girlfriend );
Echo "<br/> ";
// Undo girlfriend. Because girlfriend is private, the _ unset () method is automatically called.
// Unset ($ xudianyang-> girlfriend );
Isset ($ xudianyang-> girlfriend );
Echo "<br/> ";
// Directly output an object and automatically call the _ toString () method
Echo $ xudianyang;
Echo "<br/> ";
$ Xu = clone $ xudianyang;
Echo $ xu-> sex;
Echo "<br/> ";
// Automatically call the _ call () method because the method does not exist.
$ Xudianyang-> walk ();
Echo "<br/> ";
// Serialize the object and automatically call the _ sleep () method
$ A = serialize ($ xudianyang );
Echo "<br/> ";
Echo "serialized result:". $;
// Deserialize the object and automatically call the _ wakeup () method
Echo "<br/> ";
$ B = unserialize ($ );
// Deserialization result
Echo $ B-> sex;
Echo "<br/> ";
Eval ('$ c ='. var_export ($ xudianyang, true ).";");
Echo $ c-> var1;
?>

CheShi. class. php

<? Php
Class CheShi {
Public $ var1;
Public $ var2;
}
?>

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.