Php Magic Method Function and Usage example Analysis _php skill

Source: Internet
Author: User

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.

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.