Php object-oriented data hiding

Source: Internet
Author: User
What is Data Hiding? Some people will find it hard to understand. In the previous article, when we introduced the class, we mentioned that the keyword used to define variables is public, but there are more than public, private, protected, static, and final, these keywords are used to limit the access permissions of class members, including variables and methods. Next, we will explain the usage of the first three keywords. What is Data Hiding?

Some people will find it hard to understand. In the previous article, when we introduced the class, we mentioned that the keyword used to define variables is public, but there are more than public, private, protected, static, and final, these keywords are used to limit the access permissions of class members, including variables and methods. Next, we will explain the usage of the first three keywords.

Tip:

It is the same to limit member variables and member methods to the use of keywords. So here we only use the member variables as an example to illustrate the different usage of these keywords. The same applies to member methods.

Next, we will introduce the following keywords:

(1) public

Chinese translation is public. As the name suggests, it is public and does not need to hide data information. It can be called by other classes and objects at any position (inside or outside the class) in the program. Subclass can inherit and use all the public members of the parent class.

Before explaining this chapter, the declaration of variables and methods is both public, and all methods are public by default. Therefore, the call to variables and methods is very confusing. To solve this problem, we will describe the second keyword private.

We will not give an example here because the public is used previously.

(2) private

Chinese translation is private and private. Variables and methods modified by private can only be called and modified within the class, but cannot be accessed outside the class. It is not allowed in subclass.

Private instance display:

 Name = $ name;} public function getName () {return $ this-> name ;}} class Preson1 extends Preson {}$ preson = new Preson (); // correct usage echo 'My name is '; $ preson-> setName ("Britz "); // assign values to echo $ preson-> getName () through the Access member method; // access by using the access member method // return 'my name' in the wrong way '; echo Preson: $ name;

In the above example, modification and access to the private variable $ name can only be implemented by calling the member method. If you call it directly, an error occurs.

(3) protected

The meaning of protection. The private keyword can completely hide the data. except in this class, it cannot be called in other places, nor can it be called in subclass. However, in some cases, some variables must be called in the subclass, but encapsulation is required for other classes. At this time, our protected keyword will be used.

A class member modified with protected can be called in this class and subclass, but cannot be called elsewhere.

Protected instance display:

 '; Echo' My name is: '. $ this-> name; echo'
';}}$ Preson = new Preson1 (); $ preson-> showName (); echo' you called the error, so I won't return an error here :'; $ preson-> name = 'delav ';

In the above example, a variable is first declared with the keyword protected, then called in the subclass method, and then directly called outside the class, the results are clear at a glance.

Tip:

Although PHP does not impose mandatory rules and requirements on the keyword for modifying variables, the private and protected keywords are generally used to modify variables in terms of features and design of the surface object, to prevent variables from being directly modified and called outside the class.

These are the details of php Object-Oriented Data Hiding. For more information, see other related articles in the first PHP community!

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.