PHP Object-oriented data hiding

Source: Internet
Author: User
What is data hiding?

See this some people will feel quite not understand. In the previous article, when we introduced the class, we said that the keyword used to define the variable is public, but not only this one, but also public, private, protected, static, and final, which are used to qualify class members, including variables and methods, for access. Next, let's start by explaining the usage of the first three keywords.

Friendly tips:

Qualification of member variables and member methods is the same for the use of keywords. So here we just use the member variables as an example to illustrate the different uses of these keywords. The same applies for member methods.

Here, let's describe these key words in detail:

(1) Public

The Chinese translation is public. As the name implies, is public, do not need to hide the data information. Can be called from anywhere in the program (within the class, outside the Class) by other classes and objects. Subclasses can inherit and use all public members in the parent class.

Before we explain this chapter, we define the declarations of variables and methods to be public, and all methods are public by default, so calls to variables and methods are confusing. To solve this situation, we are going to tell the second keyword private.

Because the public is used before, we don't give an example here.

(2) Private

Chinese translation for private, private. Variables and methods that are modified by private can only be called and modified within the class in which they are located, and cannot be accessed outside of the class. It is also not possible in subclasses.

Private instances Show:

<?phpheader ("Content-type:text/html;charset=utf-8"), class preson{   private $name = "Lake race";   Public Function SetName ($name) {       $this, name = $name;   }   Public Function GetName () {       return $this->name;   }} Class Preson1 extends preson{} $preson = new Preson ();//The correct way to use echo ' my name '; $preson->setname ("Britz");    Assign values by accessing the Member method Echo $preson-GetName ();      Access//Error usage by accessing member methods Echo ' my name '; Echo Preson:: $name;

In the above example, the modification and access to the private variable $name can only be achieved by invoking the Member method. If called directly, an error occurs.

(3) Protected

Chinese translation for the protection of meaning. The Private keyword can completely hide the data, except in this class, where no other can be called, nor can the subclass. But there are cases where some variables need to be called in a subclass, but for another class, encapsulation is still the case. We're going to have to use our protected keyword at this point.

Class members decorated with protected can be called in this class and in subclasses, but cannot be called anywhere else.

Examples of protected show:

<?phpheader ("Content-type:text/html;charset=utf-8"), class preson{   protected $name = ' Victor ';} Class Preson1 extends preson{public   function ShowName () {       echo ' only actually call, I will appear ';       Echo ' <br/> ';       Echo ' My name is: '. $this->name;       Echo ' <br/> ';   }} $preson = new Preson1 (); $preson-ShowName (); Echo ' You call wrong, so the following is not an error I lose: '; $preson, name = ' De Levin ';

In the above example, we first use keyword protected to declare a variable, and then call it once in the method of the subclass, and finally call it directly outside the class, the result is at a glance.

Tips:

While there are no mandatory rules and requirements for the keywords in PHP that modify variables, it is common to use the private and protected keywords to modify variables in order to prevent variables from being directly modified and invoked outside of the class, considering the characteristics and design aspects of the face object.

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.