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

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

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.