PHP Magic method functions and usage

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.