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.