This example describes the PHP magic method features and usage. Share to everyone for your reference, specific as follows:
<?php//php in the Magic Method header (' Content-type:text/html;charset=utf-8 ');
Class person{public $name;
protected $sex;
Private $salary;
The constructor method, the instantiated object is the automatically triggered method 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__; The//__tostring () public function __tostring () {return ' object must be Var_dump () '. '
The name is '. $this->name;
//__clone () automatically invokes the Public function __clone () {echo ' This object is cloned ' when using the Clone method;
$this->name= ' Dick ';
//__get () The method public function __get ($a) {echo $a. ' Property does not exist or permissions are not sufficient ' when calling a property that does not exist or has insufficient permissions to trigger automatically
//__set () The method public function __set ($name, $value) that is automatically triggered when setting a property that does not exist or has insufficient permissions {echo $name. $value;
//__isset () The method public function __isset ($name) {echo $name that is automatically triggered when a property that does not exist or has insufficient permissions is judged outside the class. ' cannot be empty '; //__call () The Magic method that is automatically triggered when accessing a method that has insufficient or non-existent permissions is public function __call ($method, $a) {echo ' The method you requested '. $method. ' Not exist ';
$this->getname ();
The Public Function getname () {echo $this->name;
}//Instantiate object $person =new person (' John ', ' Male ', 12223);
$person 1=new person (' John ', ' Male ', 12223);
$person 2=new person (' Dick ', ' 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 ();
More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.