Introduction: This is a PHP study note ------ [App of [encapsulation], one of the three main object-oriented features of PHP]. It introduces the knowledge, skills, and experience related to PhP, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 338320 'rolling = 'no'>
<? PHP/** encapsulation: One of the three main features of object-oriented programming ** 1. is to combine the object members (attributes, methods) into an independent unit, and try to hide the internal details of the object * access permission modifier public protected private * Private: private, members modified with this keyword can only be accessed inside the object (only $ this is used). ** attributes can be encapsulated: * as long as one variable, it must be used in multiple methods, this variable is declared as a member attribute. You can directly use the ** member attribute in all methods of this object, which is equivalent to the global variable ** member attribute in this object, the change in the member attribute value is actually changing the execution behavior of the method, that is, changing the object function ** if the value of the member attribute is abnormal, the function leaf of the method execution is not normal: you do not need to change or read the value of X 1 outside the object. encapsulate * and then provide a public method (you can control the attributes of object members by assigning values and values). The *** method can also be used. To encapsulate ** function: * 1. use private modification so that it can only be used internally ** 2. there are 100 methods in a class, and 95 methods are encapsulated (for the other 5 services). Only five methods can use ** 100 member attributes, all of which allow values, you cannot change the value, or you can only change the value. You cannot obtain the value. // in this case, use the following method to facilitate * magic methods related to encapsulation: ** _ set (); // when the attribute values of [private] members are directly set, the * _ Get () method is automatically called. // when the attribute values of [private] members are directly obtained, the automatically called Method * _ isset (); // when you directly use isset () to check whether the private attribute in the object exists, this method * _ unset () is automatically called (); // when you directly use unset () to delete the private attributes of an object, the method *********/class person {// X encapsulates the member attributes, you do not need to change private $ name; private $ age; priv outside the object. Ate $ sex; private _ unset ($ proname) {unset ($ this-> $ proname);} // when you directly check whether the private property of an object exists, automatically call this method // _ isset ($ proname), $ proname indicates the property name private function _ isset ($ proname) {return isset ($ this-> $ proname); // whether isset () exists} function _ construct ($ name, $ age, $ sex) {$ this-> name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} // when obtaining private member attributes, automatically call this method private function _ Get ($ proname) {// control the obtained value if ($ proname = "Age ") {if ($ this-age> 40) return $ This-> Age-10;} return $ this-> $ proname;} // The Private function _ set ($ proname, $ provalue) {// $ proname indicates the member property name, $ provalue indicates the member property value // control the setting range if ($ proname = "Age ") {if ($ provalue> 100 | $ provalue <0) return ;}$ this-> $ proname = $ provalue ;} // provides a public method to set the value of the member attribute function setage ($ age) {// controls the age range and increases the security if ($ age> 100 | $ age <0) return; $ this-> age = $ age;} // provides a public method to obtain the value of the member attribute function getage () {// control the age range if ($ this-> age <3 0) return $ this-> age; else if ($ this-> age <40) return $ this-> Age-5; else if ($ this-> age <50) return $ this-> age; elsereturn $ this-> Age-15; Provide public methods} function say () {echo "my name: {$ this-> name }, my age: {$ this-> Age}, my surname: {$ this-> sex} <br> "; // access encapsulated run () method $ this-run ();} private function run () {echo '2017 <br> '} function eat () {}// destructor function _ destruct () {}}$ p1 = new person ("zhangsan", 25, "male"); $ P2 = new person; $ P3 = new perso N; // $ P1-> age =-50; // because the age is randomly accessed from the outside, the member attributes are encapsulated to ensure security. $ P1-> setage (30); // you can set the membership attribute value $ P1-> getage (); // obtain the attribute value of a member using a method // Add the magic method _ set ($ proname, $ provalue) _ Get ($ proname ), you can directly call the member attributes $ P1-> say (); // you can call $ P1-> Run (); // Private methods cannot be called directly // Delete nameunset ($ P1-> name) in $ P1 ); // determine whether the name exists if (isset ($ P1-> name) {echo "exists <br> ";} else {echo "This member does not exist <br>" ;}?>
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/338320.html pageno: 9.