Php (13) object-oriented encapsulation

Source: Internet
Author: User
Object-oriented encapsulation: 1. it combines object members (attributes and methods) into an independent unit and tries to hide the internal details of the object publicprotectedprivate, members modified with this keyword can only be accessed within the object (only with $ this access). object-oriented encapsulation: 1 is to put the object's members (attributes, methods) combine them into an independent unit, and try to hide the internal details of the object public protectedprivate. the members modified with this keyword can only be accessed within the object (only accessed with $ this ), examples cannot be used outside the object:
Class Person {private $ name; private $ age; private $ sex; function _ construct ($ name = "", $ age = 20, $ sex = "male ") {$ this-> name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function getPro ($ name) {return $ this-> $ name;} function setAge ($ age) {if ($ age> 100 | $ age <0) {return ;} $ this-> age = $ age;} function getAge () {if ($ this-> age <30) {return $ this-> age ;} elseif ($ this-> age <40) {return $ this-> age-5;} elseif ($ this-> age <50) {return $ this-> age-10;} else {return $ this-> age-15;} function say () {echo "My name is :". $ this-> name. ", age :". $ this-> age. ", Gender :". $ this-> sex.'
';} Function _ destruct () {echo $ this-> name. ", goodbye "."
";}}$ P1 = new Person (" rayhooo ", 26," male "); $ p1-> say (); echo $ p1-> getPro ("name ").'
'; $ P1-> setAge (45); echo $ p1-> getAge ().'
';

 

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.