Simple example of definition and usage of PHP object-oriented Programming class

Source: Internet
Author: User
Tags php programming
This paper describes the definition and usage of PHP object-oriented programming class. Share to everyone for your reference, as follows:

<?phpclass person {private $name;  Private $sex;  Private $age;    function __construct ($name = "", $sex = "male", $age =) {$this->name = $name;    $this->sex = $sex;  $this->age = $age; }//is automatically called when assigning a value directly to a private property, you can mask some illegal assignment//before a version can be set to private function __set ()//The Magic Method __set () must has public vis      Ibility//Because 5.35 of the magic methods are strictly restricted by the public function __set ($propertyName, $propertyValue) {if ($propertyName = = "Sex") { if (! ($propertyValue = = "Male" | | $propertyValue = = "female")      {return;      if ($propertyValue > | | $propertyValue < 0) {return;  }}//Based on the passed-in member property name, assign the corresponding value $this-$propertyName = $propertyValue; }//used to get the private property public function __get ($propertyName) {if (Isset ($this, $propertyName)) {return ($this    $propertyName);    } else {return (NULL);    }} Public Function __isset ($propertyName) {if ($propertyName = = "Name") {return false;//return False, do not allow this property to be measured outside the object } returnIsset ($this, $propertyName); The Public Function __unset ($propertyName) {if ($propertyName = = "Name") {return;//do not allow deletion of the Name property} unset ($this-  > $propertyName); } function say () {echo $this->name.  "In speaking <br/>";  } function run () {echo "is walking <br/>"; } function __destruct () {echo "Goodbye". $this->name.  "<br/>"; }} $person 1 = new person (), $person 2 = new Person ("2"), $person 3 = new Person ("3"),//Automatically calls __set () $person 1->name = "Zhang San"; Echo $person 1->name;echo "<br/>"; Echo $person 1->say ();//__get () echo $person 1->age;echo "is called automatically. <br/> "; Var_dump (Isset ($person 1->name); echo" <br/> "; unset ($person 1->name); echo" unset------------> ". $person 1->name;//name not been unset () echo" <br/> "; $person 2 = null;? >

Results:

Zhang Sanzhang Talking 22bool (false) unset------------> Zhang San goodbye2goodbye3goodbye Zhang San

I hope this article is helpful to you in PHP programming.

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.