Encapsulation is one of the three characteristics of surface object programming, encapsulation is to combine the properties and services of an object into a separate unit, and to conceal the inner details of the object as much as possible, including two meanings:
1. Combine all the attributes of an object with all services to form an indivisible independent unit (i.e. object).
2. Information concealment, that is, as far as possible to conceal the internal details of the object, the external formation of a boundary (or form a barrier), only a limited external interface to make it contact with the outside.
The principle of encapsulation in the software reflects that the requirements of the object beyond the arbitrary access to the object's internal data (attributes), thereby effectively avoiding the external error on its "cross-infection", so that software errors can be localized, greatly reducing the difficulty of error-checking and debugging.
Use an example to illustrate, if a person's object has attributes such as age and wages, such as the nature of personal privacy is not to let others feel free to obtain, if you do not use encapsulation, then others want to know can be obtained, but if you package on the other people will not be able to obtain the attributes of encapsulation, unless you say it yourself , otherwise there is no way for others to get it.
For example, a personal computer has a password that does not allow others to log in and copy and paste it in your computer. There is a human object, height and age attributes, can only be to increase their own, can not let others arbitrarily assigned to the value and so on.
Use the Private keyword to encapsulate properties and methods:
The original member:
var $name;//The name of the person var $sex;//The person's gender var $age;//declaring the person's age function run () {...}
Change to package form:
private $name;//The person's name using the Private keyword to encapsulate the private $sex;//The person's gender using the private keyword to encapsulate the private $age; Use the Private keyword to encapsulate a person's age private function run () {...}//Take a person's way of walking using the Private keyword for encapsulation
Note: The original keyword "var" should be removed as long as there are other keywords in front of the member property.
By private, you can encapsulate a person's members (member properties and member methods). The members on the package cannot be accessed directly outside the class, and the following code generates an error if the object itself is accessible internally:
<?phpclass person{//Below is a member of the human property of the private $name;//The name of the person, the private package on the private $sex,//human gender, the private package on the private $age; The age of the person, the private package on the//This person can talk method function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;} This person can walk the way, was the private package on a private function run () {echo "This person is walking";}} Instantiate an Instance object $p1 = new Person ();//try to assign a value to a private property, the result will be error $p1->name = "Zhang San"; $p 1->sex = "male"; $p 1->age = 20;//trying to print private property, the result will be an error echo $p 1->name;echo $p 1->sex;echo $p 1->age;//attempt to print the private member method, the result will be error $p1->run (); >
The output is:
Fatal Error:cannot Access Private Property person:: $name
Fatal Error:cannot Access Private Property person:: $sex
Fatal Error:cannot Access Private Property person:: $age
Fatal Error:cannot Access Private Property person:: $name
Fatal Error:call to Private Method Person::run () from context '
As you can see from the above example, a private member cannot be accessed externally because the private member can only access it within the object itself, for example, $p 1 The object itself wants to say his private property, and in Say () This method accesses the private property, which is yes.
without any access control, the default is public and can be accessed anywhere .
This person can speak in a way that speaks his own private property, and here also can access the Private method function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;//can also access private methods here//$this->run ();}
Because the Member method say () is public, it is possible to call the Say () method outside of the class to change the above code:
<?phpclass person{//Below is a member of the human property of the private $name;//The name of the person, the private package on the private $sex,//human gender, the private package on the private $age; The age of the person, the private package on the//define a constructor method parameter for the private property name $name, gender $sex and age $age assignment function __construct ($name, $sex, $age) {//passed in through the construction method of the $ The name gives the private member property $this->name the value $this->name = $name;//The $sex passed in by the constructor method gives the private member property $this->sex the value $this->sex = $sex; /$age to the private member property $this->age by constructing the method to assign the value $this->age = $age;} This person can speak in a way that speaks his own private property, and here also can access the Private method function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;}} The construction method creates 3 objects $p1, P2, $p 3, each passing in three different arguments for name, gender, and age $p1 = new Person ("Zhang San", "male"), $p 2 = new Person ("John Doe", "female", "3"), $p = new Pe Rson ("Harry", "male", 40);//The following access to the $p1 object in the speech method $p1->say ();//The following Access $p2 object in the speech method $p2->say ();//Access $p3 object in the following way $p3->say () ;? >
The output is:
My name is called: Zhang San Sex: Male my age is: 20 my name is called: John Doe Sex: Female my age is: 30 My name is called: Harry Sex: Male My age is:
because the constructor method is the default public method (the construction method is not set to private), it can be accessed outside the class so that the object can be created using the construction method, and the constructor is also the function inside the class, so you can use the constructor method to assign the initial value to the private property. The method of Say () is the default public, so you can also access it outside, and say his own private property.
From the above example, we can see that the private members can only be used within the class, not directly from the outside of the class access, but within the class is access to the inside, so sometimes we need to be outside the class to assign the private property and read out, that is, to provide some access to the outside of the class interface, The constructor method in the example above is a form of assignment, but the constructor is only assigned when the object is created, and if we already have an existing object and want to assign a value to the existing object, then if you also pass the value in the form of the constructor method, a new object is created. This is not an existing object. So we have to do some external access to the private properties of the interface, the purpose is to be able to change and access the value of the property in the case of the object exists, but note that only need to let the external change of the property to do so, do not want to let the outside access to the property is not to do such an interface, so that can achieve the All the functions are done by the object itself, to provide as few operations as possible outside.
If you provide an interface outside the class, you can manipulate the private property by providing a set method and a Get method outside the class for the private property. For example:
Prvate $age; The private attribute Age function setage ($age)//provides a public setting age for the external method {if ($age <0 | | $age >130)//When assigning a value to an attribute, in order to avoid an illegal value set to the property return;$ This->age = $age;} function getage ()//provides a method for the external public acquisition Age {return ($this->age);}
The method above is to set and get values for a member property, and of course you can use the same method for each property to assign and value the operation, complete the access work outside the class.
"Getting Started with PHP object-oriented (OOP) programming" 9. Encapsulation (var vs. Public,protected,private)