This article mainly introduces the definition and usage of PHP object-oriented programming classes, and analyzes the definition, instantiation, _ set () and _ get () of php classes in the form of instances () for more information, see the following example to describe the definition and usage of the PHP object-oriented programming class. We will share this with you for your reference. The details are as follows:
Name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} // it is automatically called when values are directly assigned to a private property, some invalid values can be blocked. // Some Versions earlier can be set to private function _ set () // The magic method _ set () must have public visibility // because 5.35 strictly limits the magic method's public function _ set ($ propertyName, $ propertyValue) {if ($ propertyName = "sex ") {if (! ($ PropertyValue = "male" | $ propertyValue = "female") {return;} if ($ propertyValue> 150 | $ propertyValue <0) {return ;}} // assign the corresponding value $ this-> $ propertyName = $ propertyValue based on the passed member attribute name ;} // obtain 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, this attribute cannot be determined outside the object} return isset ($ this-> $ propertyName);} public function _ unset ($ propertyName) {if ($ propertyName = "name") {return; // The name attribute cannot be deleted} unset ($ this-> $ propertyName);} function say () {echo $ this-> name. "Talking
";} Function run () {echo" is walking ·
";} Function _ destruct () {echo" goodbye ". $ this-> name ."
";}}$ Person1 = new Person (); $ person2 = new Person (" 2 "); $ person3 = new Person (" 3 "); // automatically calls _ set () $ person1-> name = "Zhangsan"; echo $ person1-> name; echo"
"; Echo $ person1-> say (); // automatically calls _ get () echo $ person1-> age; echo"
"; Var_dump (isset ($ person1-> name); echo"
"; Unset ($ person1-> name); echo" unset ------------> ". $ person1-> name; // name is not returned by unset () echo"
"; $ Person2 = null;?>
Result:
Zhang San is talking about 22 bool (false) unset ------------> Zhang San goodbye2goodbye3goodbye Zhang San
I hope this article will help you with PHP programming.
For more articles on definitions and simple examples of PHP object-oriented programming classes, refer to PHP Chinese network!