The Magic method is unique to PHP's object-oriented features. They are triggered in a particular case, they start with a double underscore, you can interpret them as hooks, and you can easily implement the PHP object-oriented overload using the pattern method (overloading), which dynamically creates class properties and methods. Many of the magic methods appear in pairs, and the following lists all of the current mode methods in PHP.
1.__construct,__destruct
__constuct is invoked when the object is constructed;
__destruct explicitly destroys the object or is invoked at the end of the script;
2.__get,__set
__set is invoked when assigning values to inaccessible or non-existent attributes
__get is invoked when a property is read that is inaccessible or does not exist
3.__isset,__unset
__isset is called when isset () or empty () is invoked on a property that is inaccessible or nonexistent.
__unset is invoked when unset to an inaccessible or nonexistent property
4.__call,__callstatic
Called when an __call or nonexistent method is invoked
Called when __callstatic calls a static method that is inaccessible or does not exist
5.__sleep,__wakeup
__sleep is called when using serialize and is useful when you don't need to save all the data for a large object
__wakeup is invoked when using unserialize and can be used for initialization of objects
6.__clone
Invoked when object clone is used to adjust the clone behavior of an object
7.__tostring
Called when a class is converted to a string
8.__invoke
Called when an object is invoked as a function
9.__set_state
This static method is invoked when Var_export () is invoked to export the class. Use the return value of the __set_state as the return value of the Var_export.
10.__debuginfo
Called when the Var_dump () Print object is invoked (when you do not want to print all properties) apply to the PHP5.6 version
The PHP Magic method uses the following example:
<?php class Magic {public $var = ' Test '; constructor to invoke the public function __construct () {echo ' __construct called ' when the object is created.
Php_eol; }//A reference to an object is deleted, the object is destroyed, the call to exit () is invoked, and the public Function __destruct () {echo ' __destruct called ') is called when the script closes.
Php_eol;
The Public Function __set ($name, $value) {echo $name is invoked when an unreachable or nonexistent property assignment is given. $value; Echo ' __set called '.
Php_eol;
//The Public Function __get ($name) {echo $name is invoked when the property is not accessible or not present; Echo ' __get called '.
Php_eol; The Public Function __call ($name, $arguments) {echo $name is invoked when an inaccessible or nonexistent method is invoked. '-' .
Implode (', ', $arguments); Echo ' __call called '.
Php_eol; The public static function __callstatic ($name, $arguments) {echo $name is invoked when an inaccessible or nonexistent static method is invoked. '-' .
Implode (', ', $arguments); Echo ' __callstatic called '.
Php_eol;
The Public Function __isset ($name) {echo $name is invoked when isset () or empty () is invoked on an inaccessible or Non-existent property; Echo ' __isset called '.
Php_eol;
return true;The Public Function __unset ($name) {echo $name is invoked when unset to an inaccessible or non-existent property; Echo ' __unset called '.
Php_eol; When//serialize is invoked, it is useful when you do not need to save all the data for large objects public Function __sleep () {echo ' __sleep called '.
Php_eol;
Return Array (' var1111111111 '); When//unserialize is invoked, it can be used to do initialization of some objects public function __wakeup () {echo ' __wakeup called '.
Php_eol;
$this->var = ' test after wakeup '; //When a class is converted to a string, the public function __tostring () {return ' __tostring called ' is invoked.
Php_eol; ///Is invoked when object clone is used to adjust the clone behavior of the object public function __clone () {echo ' __clone called '.
Php_eol; The Public Function __invoke () {echo ' __invoke called ') is invoked when the object is called in a functional manner.
Php_eol; This static method is invoked when Var_export () is invoked to export the class.
Use the return value of the __set_state as the return value of the Var_export. public static function __set_state ($arr) {return ' __set_state called '.
Php_eol; //When calling Var_dump () to print an object (when you do not want to print all properties) applies to the PHP5.6 version public Function __debuginfo ($arr) {echo ' __debuginfo called ' .Php_eol;
return array (' var ' => ' Test fro __debuginfo '); }} $m = new Magic (); __construct () is called $m->not_exist_property = test; __set () called Echo $m->not_exist_property;//__get () is invoked $m->ABC (1,2,3); __call () is invoked to echo isset ($m->not_exist_property); __isset () is invoked to return the bool value unset ($m->not_exist_property); __unset () is invoked to echo $tmp = serialize ($m); __sleep () is called unserialize ($tmp); __wakeup () is called $m 1 = clone $m; __clone () is invoked, the object is referenced by default, and using the Clone keyword enables the object to replicate $m (); __invoke () eval (' $m 2 = '. Var_export ($m, True). ';' );
Var_dump ($m 2);
Var_dump ($m); The last __destruct () is called/* results: __construct called Not_exist_property-test__set called Not_exist_property__get called Abc-1, Called Not_exist_property__isset called 1not_exist_property__unset called __sleep called 2,3__call: "O:5": Magic : "var1111111111"; N;} __wakeup called __destruct called __clone called __invoke called string (m) "__set_state called" Class Magic#1 (1) {Pub Lic $var => String (4) "TeSt "} __destruct called __destruct called * *