This article mainly introduces the function and usage of PHP magic method, combined with the example form simple analysis of PHP object-oriented programming common magic methods of the function and related use skills, the need for friends can refer to the next
Specific as follows:
<?php//php Magic Method Header (' Content-type:text/html;charset=utf-8 '), class person{public $name; protected $sex; Private $salary; Constructor method, instantiating the object is the method of automatic triggering 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 ' object must be Var_dump () '. ' Name is '. $this->name; }//__clone () automatically calls the Public function __clone () {echo ' This object is cloned when using the Clone method; $this->name= ' John Doe '; }//__get () when calling a method that does not exist or is not sufficient to automatically trigger the public function __get ($a) {echo $a. ' attribute does not exist or is not sufficient '; }//__set () When setting a property that does not exist or has insufficient permissions, the Public function __set ($name, $value) {echo $name. $value; //__isset () The method that is automatically triggered when a property outside the class is judged to be non-existent or insufficient, public function __isset ($name) {echo $name. ' cannot be empty '; }//__call () when accessing a method that has insufficient or no permissions, it automatically triggers the Magic method public function __call ($method, $a) {echo ' The method you requested '. $meThod. ' does not exist '; $this->getname (); } public Function GetName () {echo $this->name; }}//instantiates the object $person=new person (' Zhang San ', ' Male ', 12223), $person 1=new person (' Zhang San ', ' Male ', 12223 '),//$person 2=new person (' John Doe ', ' female ', 11111); Var_dump ($person); Var_dump ($person 1); exit; $person->getclassname (); $person->getmethod ();//echo $ Person->name;//echo $person; $person 1= $person; The same object//var_dump ($person);//var_dump ($person 1); $person 2=clone $person; Cloning produces a different object//var_dump ($person);//var_dump ($person 2);//echo $person 2->name;//$person->salary=10000;//var _dump (Isset ($person->salary));//$person->getname ();
Summary: The above is the whole content of this article, I hope that everyone's learning has helped. At the same time also hope that we support topic.alibabacloud.com.