9. Encapsulation of
Encapsulation is one of the three characteristics of object-oriented programming, and encapsulation is the combination of object attributes and services into a
Independent of the same unit, and as far as possible conceal the inner details of the object, containing two meanings: 1. The entire property of the object and the full
Department services together to form an indivisible unit (ie, object). 2. Information concealment, that is, as far as possible concealment of
The internal details of the image, which form a boundary (or form a barrier), leaving only the limited external interface to make the external
Ministry of Communication.
The principle of encapsulation is reflected in the software: the requirement to make parts of objects outside of the object inaccessible to the object's internal data
(attribute), so as to effectively avoid the external error on its "cross infection", so that software errors can be localized, greatly reduce
It is difficult to check and debug less correctly.
Use an example to illustrate that if a person's object has attributes such as age and salary, such personal privacy attributes
is something that you don't want other people to be able to get, if you don't use encapsulation, then people want to know, but like
After you've encapsulated it, there's no way to get the encapsulated property, unless you say it yourself, otherwise people don't.
method to get
For example, the PC has a password, do not want to let other people randomly landing, in your computer copy and
Paste. There is a person like this object, height and age attributes, can only grow on their own, can not let others arbitrary
assignments, and so on.
Use the Private keyword to encapsulate properties and methods:
Original members:
var $name; Name of the person who made the declaration
var $sex; Statement of the gender of the person
var $age; Declaration of the age of the person
function run () {...}
In the form of a package:
Private $name; To encapsulate a person's name with the private keyword
Private $sex; To encapsulate a person's sex with a private keyword
Private $age; Use the Private keyword to encapsulate the age of the person
Private function Run () {...}//to encapsulate a person's way of walking using the Private keyword
Note: As long as there are other keywords in front of the member property, you should remove the original keyword "var".
You can encapsulate people's members (member properties and member methods) with private. The member on the package cannot
is accessed directly outside of the class, only within the object itself, and the following code generates an error:
Code fragment
Copy Code code as follows:
Class person{
The following are the member properties of the person
Private $name; The name of the man, is sealed in private.
Private $sex; The sex of the human being is packaged in private.
Private $age; The age of the person, is sealed in private
The way this man can talk.
function say () {
echo "My name is:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
The way this man can walk, is encased in private.
Private function Run () {
echo "This man is walking";
}
}
Instantiate an instance object of a person
$p 1=new person ();
Attempting to assign a value to a private property, the result is an error
$p 1->name= "John";
$p 1->sex= "Male";
$p 1->age=20;
An attempt to print a private property results in an error
echo $p 1->name. " <br> ";
echo $p 1->sex. " <br> ";
echo $p 1->age. " <br> "
An attempt to print a private member method will result in an error
$p 1->run ();
The output results are:
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 the context '
As you can see from the above example, private members cannot be accessed externally because private members can only
Internal access, for example, $p 1 The object itself wants to speak out of his private properties, and to visit the Say () method
Ask the private property, this is OK. (without any access control, the default is public, anywhere can visit
Ask
Code fragment
Copy Code code as follows:
This person can speak the way to say their own private properties, where you can also access the private method
function say () {
echo "My name is:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
Private methods can also be accessed here
$this->run ();
}
Since the Member method say () is public, we can invoke the say () method outside the class to change the
Code
Code fragment
Copy Code code as follows:
Class person{
The following are the member properties of the person
Private $name; The name of the man, is sealed in private.
Private $sex; The sex of the human being is packaged in private.
Private $age; The age of the person, is sealed in private
Defines a constructor parameter that is assigned to a private property name $name, Sex $sex, and age $age
function __construct ($name, $sex, $age) {
$name to the private member property $this->name by constructing the method
$this->name= $name;
$sex to the private member property $this->sex by constructing the method
$this->sex= $sex;
$age to the private member property $this->age by constructing the method
$this->age= $age;
}
This person can speak the way to say their own private properties, where you can also access the private method
function say () {
echo "My name is:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
}
Create 3 Objects $p1, P2, $p 3 by constructing a method, passing in three different arguments to name, gender, and age, respectively.
$p 1=new person ("John", "Male", 20);
$p 2=new person ("Dick", "female", 30);
$p 3=new person ("Harry", "male", 40);
The following accesses the speech method in the $p1 object
$p 1->say ();
The following accesses the speech method in the $p2 object
$p 2->say ();
The following accesses the speech method in the $p3 object
$p 3->say ();
The output results are:
My name is: John Sex: Male My age is: 20
My name is: Dick Sex: Female my age is: 30
My name is: Harry Sex: Male My age is: 40
Because the construction method is the default public method (the constructor is not set to private), it is possible to visit the outside of the class
Ask, so you can use the construction method to create the object, and the construction method is also the function inside the class, so you can use the construction
Create a method to assign an initial value to a private property. The Say () method is the default public, so it can be accessed on the outside, saying
Out of his own private property.
As we can see from the above example, private members can only be used inside the class, not directly from the class outside.
Access, but there is permission to access the inside of the class, so sometimes we need to assign a value to the private property outside the class and
read out, that is to give the outside of the class to provide some accessible interface, in the example of the construction method is an assignment of the shape
, but the construction method is only assigned when the object is created, and if we already have an object in existence, we want to
An existing object assignment, this time, if you also use the construction method to pass the value of the form of the value, then create a
The new object, not the existing object. So we're going to do some of the private properties that can be accessed externally
Interface, the purpose is to change and access the value of the property in the presence of the object, but note that only the need to let the outside
The properties of the changed section do not want to let the outside access of the property is not to do such an interface, so you can achieve the encapsulated
Objective, all functions are done by the object themselves, to provide the outside with as little operation as possible.
If you provide an interface to a class outside, you can provide the private property with a set method and a method outside the class to manipulate the private
has attributes. For example:
Code fragment
Copy Code code as follows:
Prvate $age; Private attribute Age
function Setage ($age) {
Provide an external method of setting the age for the public
if ($age <0 | | | $age >130)///when assigning a value to a property, to avoid illegal values set to the property
Return
$this->age= $age;
}
function Getage () {//provides a public access age to the outside
Return ($this->age);
}
The method above is to set and get values for a member property, but you can also use the same method for each attribute
It carries out the assignment and the value operation, completes the access work outside the class