Php magic method function and usage instance analysis, magic instance analysis
This article describes the functions and usage of php magic methods. We will share this with you for your reference. The details are as follows:
<? Php // php magic method header ('content-type: text/html; charset = UTF-8 '); class Person {public $ name; protected $ sex; private $ salary; // constructor. The instantiated object is the automatically triggered public function _ construct ($ name, $ sex, $ salary) {$ this-> name = $ name; $ this-> sex = $ sex; $ this-> salary = $ salary;} // magic constant _ CLASS _ public function getClassName () {echo _ CLASS __;}// _ FUNCTION _ public function getMethod () {echo _ FUNCTION __;}// _ tostring () public function _ tostring () {return 'the object must use var_dump ()'. 'name is '. $ this-> name;} // _ clone () the public function _ clone () is automatically called when the clone method is used () {echo 'the object is cloned '; $ this-> name = 'lily';} // _ get () public function _ get ($ a) {echo $. 'Property does not exist or permission is insufficient ';} // _ set () the public function _ set ($ name, $ value) {echo $ name. 'Is '. $ value;} // _ isset () the public function _ isset ($ name) method is automatically triggered when an attribute does not exist or has insufficient permissions is determined outside the class) {echo $ name. 'cannot be blank';} // _ call () when a method with insufficient or nonexistent permissions is accessed, the magic method public function _ call ($ method, $ a) {echo 'method you requested '. $ method. 'nonexistent '; $ this-> getname ();} public function getname () {echo $ this-> name ;}} // instantiate the object $ person = new Person ('zhang san', 'mal', 12223); $ person1 = new Person ('zhang san', 'mal', 12223 ); // $ person2 = new Person ('lily', 'female', 11111); var_dump ($ person); var_dump ($ person1); exit; $ person-> getClassName (); $ person-> getMethod (); // echo $ person-> name; // echo $ person; $ person1 = $ person; // The same object // var_dump ($ person); // var_dump ($ person1); $ person2 = clone $ person; // The clone generates different objects // var_dump ($ person); // var_dump ($ person2); // echo $ person2-> name; // $ person-> salary = 10000; // var_dump (isset ($ person-> salary); // $ person-> getName ();