Class Magic { Public $var = ' Test '; Constructor that is called when the object is created Public Function __construct () { Echo ' __construct called '. Php_eol; } A reference to an object is deleted, the object is destroyed, a call to exit () is called, and the script is closed Public Function __destruct () { Echo ' __destruct called '. Php_eol; } Called when an attribute assignment is not accessible or does not exist Public Function __set ($name, $value) { echo $name. '-' $value; Echo ' __set called '. Php_eol; } Called when the read is not accessible or the property is not present Public Function __get ($name) { Echo $name; Echo ' __get called '. Php_eol; } Called when a method is called that is not accessible or does not exist Public Function __call ($name, $arguments) { Echo $name. '-' . Implode (', ', $arguments); Echo ' __call called '. Php_eol; } Called when a static method is called that is not accessible or does not exist public static function __callstatic ($name, $arguments) { Echo $name. '-' . Implode (', ', $arguments); Echo ' __callstatic called '. Php_eol; } Called when isset () or empty () is called for an unreachable or nonexistent property Public Function __isset ($name) { Echo $name; Echo ' __isset called '. Php_eol; return true; } Called when an unset property is not accessible or not present Public Function __unset ($name) { Echo $name; Echo ' __unset called '. Php_eol; } Serialize is called when you don't need to save all the data for a large object. Public Function __sleep () { Echo ' __sleep called '. Php_eol; Return Array (' var1111111111 '); } Unserialize is called when it is used to initialize objects. Public Function __wakeup () { Echo ' __wakeup called '. Php_eol; $this->var = ' test after wakeup '; } Called when a class is converted to a string Public Function __tostring () { Return ' __tostring called '. Php_eol; } Called when the object clone is being used to adjust the cloning behavior of the object Public Function __clone () { Echo ' __clone called '. Php_eol; } Called when an object is called in a functional manner Public Function __invoke () { Echo ' __invoke called '. Php_eol; } This static method is called when the Var_export () export class is called. Use the return value of __set_state as the return value of Var_export. public static function __set_state ($arr) { Return ' __set_state called '. Php_eol; } Called when a var_dump () print object is called (when you do not want to print all properties) for 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 () is called echo $m->not_exist_property;//__get () is called $m->abc (a); __call () is called Echo isset ($m->not_exist_property); __isset () is called and returns a bool value unset ($m->not_exist_property); __unset () is called echo $tmp = serialize ($m); __sleep () is called Unserialize ($TMP); __wakeup () is called $m 1 = clone $m; __clone () is invoked, the object is passed by default, and the Clone keyword is used to implement object replication $m (); __invoke () Eval (' $m 2 = '. Var_export ($m, True). ';' ); Var_dump ($m 2); Var_dump ($m); Last __destruct () is called /* Results: __construct called Not_exist_property-test__set called Not_exist_property__get called Abc-1,2,3__call called Not_exist_property__isset called 1not_exist_property__unset called __sleep called O:5: "Magic": 1:{s:13: "var1111111111"; N;} __wakeup called __destruct called __clone called __invoke called String ("__set_state called") " Class Magic#1 (1) { Public $var = = String (4) "Test" } __destruct called __destruct called */ |