PHP Object-oriented (OOP)-Encapsulation

Source: Internet
Author: User

Encapsulation is one of the three characteristics of the object programming, the encapsulation is to combine the object's attribute and service into a separate same unit, and conceal the internal details of the object as far as possible, including two meanings:

1. Combine all the attributes of the object with all the services to form an indivisible independent unit (i.e. object).

2. Information concealment, that is, to conceal the internal details of the object as far as possible, to form a border (or to form a barrier), leaving only a limited external interface to connect with the outside.

The principle of encapsulation is reflected in the software: The requirements of the object can not be arbitrary access to the object's internal data (attributes), so as to effectively avoid the external error on its "cross infection", so that software errors can be localized, greatly reducing the difficulty of error checking and scheduling.

Let's use an example to illustrate that if someone has an attribute of age and salary in their object, the attributes of personal privacy like this are not meant to be available to others at will, and if you do not use encapsulation, then others will get it, but if you encapsulate it, others will have no way to get the encapsulated attribute unless you tell it yourself. , otherwise others have no way to get.

For example, a personal computer has a password and does not want anyone else to log in and copy and paste it into your PC. There is a person like this object, height and age attributes, can only be their own to increase, can not let others arbitrarily assigned value and so on.

Use the Private keyword to encapsulate properties and methods:

<?php
Class Person
{
The following are the member properties of the person
Private $name; The name of the person, is a private package on the
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 called:". $this->name. "Sex:". $this->sex. "My Age is:". $this->age;
}
}

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 ();
?>

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 members on the package cannot be accessed directly from outside the class, only the objects themselves can be accessed,

without any access control, the default is public and can be accessed anywhere .

Because the constructor method is the default public method (the constructor is not set to private), it can be accessed outside of the class so that the object can be created using the constructor method, and the constructor is also a function within the class, so you can assign an initial value to the private property by using the constructor method. The Say () method is the default public, so it can be accessed from the outside, stating his own private properties.

As we can see from the above example, private members can only be used internally within a class and cannot be accessed directly from outside the class, but are privileged to access within the class, so sometimes we need to assign and read the private attribute outside of the class, which is to provide some accessible interface to the outside of the class. The construction method in the example above is a form of assignment, but the construction method is only assigned when the object is created, and if we already have an existing object and want to assign a value to the object that exists, then if you also use the constructor method to pass the value in the form of a value, a new object is created. It's not the object that already exists. So we're going to do some interfaces that can be accessed externally for proprietary properties, the goal is to change and access the value of the property in the presence of the object, but be aware that only the attributes that need to be externally changed do so, and that the properties that do not allow the outside access do not have such an interface, so that the encapsulation is achieved. All functionality is done by the object itself, providing the outside with as little operation as possible.

If you provide an interface to a class outside, you can manipulate private properties by providing a set of methods and fetching methods outside the class for private properties. For example: Prvate $age; Private attribute Age
function Setage ($age) {//provides an external method for setting the age of 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 above method is to set and get values for a member property, and of course you can assign and value the same method for each attribute, and do the access work outside the class.


The above content from "Fry Peanuts" eldest brother, thank Brother to share.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.