<?phpheader (' Content-type:text/html;charset=utf-8 ');/* * __construct () Construction method * Call * __destruct () automatically when object is initialized * automatically call * __get () when the object frees resources * Automatically call the * __set () setting value when taking a private property value * automatically call * __isset () when setting a private property value * in using Isset () Automatically call * __unset () when determining if a private property is present () to release the value * automatically call * __tostring () when the private property is freed with Unset () The __call () is called when a class is used as a string to respond to * __call () Call an inaccessible method in an object. * __callstatic () __callstatic () is called when a non-accessible method is called in a static manner. :: * __sleep () method * __wakeup () called automatically when serializing Serialize () Methods that are called automatically when crossdress is * __set_state () This static method is called when the var_export () export class is called. * __invoke () the __invoke () method is called automatically when an attempt is made to invoke an object in a way that invokes a function. * __autoload () The only one that is defined outside the class auto-load This method is automatically called whenever a class needs to be loaded in this script (the class name must be used). Summary of the * * Magic Method: * 1. Automatic call, but different magic methods, have their own calling timing * 2. All in the way of * 3. All Magic methods, method names are fixed * 4. If you do not write, there is no default function. * * __tostring () * 1. Is the method that is called automatically when you export an object reference directly using echo print printf * 2. Place the basic information of the object inside the __tostring () method, forming a string return. The * 3.__tostring () method cannot have parameters. * * Clone object * 1. Use clone to copy an Object * * __clone () Magic method * 1. Is the method * 2 that is called automatically when cloning an object. Function: As with the constructor method, it is the initialization of the newly cloned object * * __call () * 1. Is the method * 2 that is called automatically when a method that is not stored in an object is called. There are two parameters, the first parameter is the method name of the non-existent method called, and the second parameter is the method parameter that calls the nonexistent method * function: * 1. You can write a message * 2. The function of the method is similar, but the method name is different, this way can be used to complete the * * * object serialization (serialized) * 1. Convert an object to a string---serialization * __sleep () * action: * can set properties of objects that need to be serialized * as long as in this method, an array is returned, the property name is declared in the array, and the property name is serialized. * no declared attribute names, no serialization * default this method, all serialization * 2. To convert a string to an object---crossdress the line * __ WakeUp () automatically invoke * function when crossdress: * object to serialize the object back, the initialization of the object, equivalent to __construct () * serialize () The function checks if there is a magic method in the class __sleep () If present, the method is called first before performing the serialization Operation * Note (serialization time) * 1. When an object needs to be transmitted over a network. * 2. When persisting an object. * * array serialization json ----javascript object notation * * 1.eval () function--check and execute PHP code * $str = "echo ' abc ' ;"; * eval ($STR); */ class person{ public $name; public $age; public $sex; public $ Marr = array (' Eat ', ' Drink ', ' song '); call constructors when initializing // Function __construct ($name, $age, $sex) { $this->name = $name; $this->age = $age; $this->sex = $sex; } //echo call function __ when an object is ToString () { return ' My basic information is as follows: I am Jin Sha Harbor, 30 years old, male <br/> '; The } //clone object is called when function __clone () { echo ' Clones an object, this is a replica <br/> '; $this->name; $this->age=20; $this->sex; } /* //Tip Information function __call ($method, $args) { echo ' automatically calls __call<br/> '; when calling a method that does not exist in an object echo "called themethod {$method} () with the parameter: "; print_r ($args); echo "does not exist <br/>"; } */ //calls different method names function __call ($method, $args) { if (In_array ($method, $this- >marr)) { foreach ($args as $arg) echo $arg. " <br/> "; } else{ echo "The method you are calling {$method} () does notExistence "; } } static Function __callstatic ($method, $args) { echo "The static method you called { $method} does not exist "; } function say () { echo ' I am Tianjin Harbour <br/> '; } //calling function _ when serializing serialize _sleep () { echo ' Specifies the serialized object property name <br/> '; return array (' name ', ' age ');//must return an array // Specifies that only the two object properties of name and age are serialized //o:6: "Person": 2:{s:4: "Name"; s:12: "Jin Sha Harbour"; S:3: " Age "; i:23;} } //crossdress call when line unserialize Function __wakeup () { echo ' crossdress automatically call <br/> ' when line-up; //setting the value of a member property when crossdress a row $this->age = 43; } // Var_export Export class Information static function __set_state ($arr) {//parameter is array echo ' var_export automatically call <br/> '; when exporting class information $p = new person ("Small champion", 33, "female"); $p->name = $arr [' name ']; $p->age = $arr [' Age ']; $p->sex = $arr [' sex ']; return $p; } // Call function method Call object $p (); function __invoke ($a, $b, $c) { echo ' Call the function automatically when calling an object '. $a. $b. $c. ' <br/> '; } }//originally $p = new person (' Jin Sha Harbor ', 23, ' Male '),//tostring () echo $p;//__call () $p->eat (' Fish ', ' meat '), $p->drink (' tea ', ' wine '), $p->song (' song ', ' Play '),//Copy __clone () $a = clone $p; echo $ a->age;//serialize object to String $str = serialize ($p);//o:6: "Person": 4:{s:4: "Name"; s:12: "Jin Sha Harbour"; S:3: "Age"; I:23;s:3: "Sex"; S:3: "Male"; s:4: "Marr"; A:3:{i:0;s:3: "Eat"; I:1;s:5: "Drink"; I:2;s:4: "Song";}} File_put_contents ("MrYang.txt", $str);echo ' object serialization succeeded, saved to Mryang file '; $arr = array ( ' name ' => ' Jin Sha Harbour ', ' age ' => 32, ' sex ' => ' male ', ) //array Serialization {"Name": "\u6d25\u6c99\u6e2f\u6e7e", "age": +, "sex": " Male "} $str = json_encode ($arr); echo $str; //array crossdress turn objects $parr = json_ Decode ($STR); var_dump ($parr);//becomes an object /* object ( StdClass) [1] public ' name ' => string ' Sabah Harbour ' (length=12) public ' age ' => int 32 public ' sex ' => string ' male ' (length=4) */ //becomes an object echo $parr->name; //to group to add a second parameter true $parr = Json_decode ($str, true); var_dump ($parr);//var_export __set_state () $p = new person ("Jin Sha Harbour", 20, "male");//The set of properties can be transmitted to $arr $p->name = ' volcanic island '; $p->age = 500; $p->sex = ' male '; eval (' $b = '. Var_export ($p, true). '; '); /add Ture to String var_dump ($b);//class information automatically calls the person when the set value in the __set_state ()//Call Function mode $p (+/-);//__callstatic calls a static method that does not exist: Speak ();//__autoload automatically loads the class file Function __autoload ($classname) { include when calling a method in the class $classname. Class.php ';} Test::laugh ();
Object crossdress include ' index.php ';//Read file content string $str = file_get_contents (' MrYang.txt ');//Crossdress line string to Object $p = Unserialize ($STR); $p- >say (); Echo $p->age;
This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1844991
PHP Object Magic Method