Php learning notes: application of [encapsulation], one of the three main object-oriented features of php. Copy the code as follows :? Php ** encapsulation: one of the three main characteristics of object orientation ** 1. it combines object members (attributes and methods) into an independent unit and hides them as much as possible.
The code is as follows:
/*
* Encapsulation: one of the three main characteristics of object-oriented
*
* 1. Combine the members (attributes and methods) of an object into an independent unit and hide the internal details of the object as much as possible.
* Access permission modifier public protected private
* Private: private. members modified with this keyword can only be accessed within the object (only accessed with $ this)
*
* Attributes can be encapsulated:
* If a variable needs to be used in multiple methods, the variable is declared as a member attribute and can be directly used in all methods of this object.
*
* Member attribute, which is equivalent to the global variable in this object
*
* All member attributes are used in the method. The change of member attribute values 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 abnormal.
*
* Function: You do not need to change or read its value outside the object.
* 1. encapsulation
* Provide a public method (assign values and values to the object member attributes by using the method)
*
*
* Methods can also be encapsulated.
*
* Function:
* 1. use the private modifier 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 be used.
*
* For attributes of 100 members, the value can be set and cannot be changed. Alternatively, the value can be changed only. The value cannot be obtained. // in this case, the following method is more convenient.
* Encapsulation-related magic methods:
*
* _ Set (); // The method automatically called when the [private] member attribute value is directly set.
* _ Get (); // The method automatically called when the [private] member attribute value is directly obtained.
* _ Isset (); // This method is automatically called when you directly use isset () to check whether a private property exists in an object.
* _ Unset (); // The method automatically called when you directly use unset () to delete private attributes of an object.
*
*
*
*
*
*
*
*/
Class Person {
// X encapsulates the member attributes and does not need to be changed outside the object
Private $ name;
Private $ age;
Private $ sex;
Private _ unset ($ proName ){
Unset ($ this-> $ proName );
}
// This method is automatically called when you directly check whether a private attribute exists in an object.
// _ Isset ($ proName), $ proName indicates the attribute name
Private function _ isset ($ proName ){
Return isset ($ this-> $ proName); // isset () returns whether or not it exists
}
Function _ construct ($ name, $ age, $ sex ){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> sex = $ sex;
}
// This method is automatically called when private member attributes are obtained.
Private function _ get ($ proName)
{
// Control the obtained value
If ($ proName = "age "){
If ($ this-age> 40)
Return $ this-> age-10;
}
Return $ this-> $ proName;
}
// This method is automatically called when private member attributes are set.
Private function _ set ($ proName, $ proValue ){
// $ ProName indicates the member attribute name, and $ proValue indicates the member attribute 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 member attributes.
Function setAge ($ age ){
// Control the age range to increase security
If ($ age> 100 | $ age <0)
Return;
$ This-> age = $ age;
}
// Provides a public method to obtain the attribute value of a member.
Function getAge (){
// Control the age range
If ($ this-> age <30)
Return $ this-> age;
Else if ($ this-> age <40)
Return $ this-> age-5;
Else if ($ this-> age <50)
Return $ this-> age;
Else
Return $ this-> age-15;
Provide public methods}
Function say (){
Echo "my name: {$ this-> name}, my age: {$ this-> age}, my surname: {$ this-> sex}
";
// Access the encapsulated run () method
$ This-run ();
}
Private function run (){
Echo '100
'
}
Function eat (){
}
// Destructor
Function _ destruct (){
}
}
$ P1 = new Person ("zhangsan", 25, "male ");
$ P2 = new Person;
$ P3 = new Person;
// $ P1-> age =-50; // because the age is randomly accessed from outside, the member attributes are encapsulated to ensure security.
$ P1-> setAge (30); // you can set the attribute value of a member.
$ P1-> getAge (); // Obtain the attribute value of a member using a method.
// You can directly call the member attributes by adding the magic method _ set ($ proName, $ proValue) _ get ($ proName ).
$ P1-> say (); // call
$ P1-> run (); // private methods cannot be called directly
// Delete the name in $ p1
Unset ($ p1-> name );
// Determine whether the name exists
If (isset ($ p1-> name )){
Echo "exists
";
} Else {
Echo "does not have this member
";
}
?>
Author: codenamed Aurora
Source: http://zizhuyuan.cnblogs.com
The http://www.bkjia.com/PHPjc/323518.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323518.htmlTechArticle code is as follows :? Php/** encapsulation: one of the three main features of object orientation ** 1. it combines object members (attributes and methods) into an independent unit and tries to hide them as much as possible...