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 ().'
';