PHP class encapsulation and inheritance details, php encapsulation details _ PHP Tutorial

Source: Internet
Author: User
PHP class encapsulation and inheritance details, php encapsulation details. PHP class encapsulation and inheritance details, php encapsulation details encapsulation encapsulates member methods and member attributes into the class, hiding the attributes and method implementation details, details on encapsulation and inheritance of public, protected, private, and other restricted PHP classes, and php encapsulation


Encapsulate member methods and member attributes in the class to hide the details of attributes and methods, and restrict the access permissions of class members through public, protected, and private. The data is protected internally, only authorized member methods can be used to encapsulate members as much as possible.

Public: methods or attributes can be accessed in any scope and are default. If an access modifier is not specified for an attribute or method, it will be public.
Protected: this class and subclass can be accessed, and external objects cannot be called.
Private: it can only be accessed in this class, and neither the subclass nor the external object can be called. Methods or attributes marked by private can be redefined in the inheritance class. each class can only see its own private method.

The three modifiers should be sorted in a descending order like this: public → protected → private in scope, because the class encapsulates some attributes and methods, this encapsulation determines the "visibility" of data ", in this way, the defined attributes and methods cannot be modified but can only be called outside the class. this is the benefit of encapsulation and improves security.
Sample code:

Class myClass {public $ public = "Public"; // public attribute protected $ protected = "Protected"; // protected attribute private $ private = "Private "; // private property function say_Hello () {// public property // Just for example, add content by yourself} $ obj = new myClass (); echo $ obj-> public; // echo $ obj-> protected; // echo $ obj-> private;

Run the above example to get a "Public", but when you remove the comments of // echo $ obj-> private;, you will get the following error:

Fatal error: Cannot access protected property myClass: $ protected in E: apachehtdocsexamplefile. php on line 13.

We can see that we cannot define the attributes of the category class at will. we don't know which members exist in this class in the "outside", because these members may be unavailable to other classes. Of course, if we must access or modify the attribute defined as "private", we can also use the system methods provided by PHP: _ get () and _ set ().


A class can inherit and have the member attributes and methods of another existing class. the inherited class is called the parent class or the base class, and the inherited class is a subclass. Use the extends keyword to implement the inheritance relationship. In general, there must be a "root cause" for inheritance. you may imagine that you have given birth to a son or daughter in the future, they will get some "things (attributes and methods)" from you, so that your "descendant" will hold all the characteristics of You (root.

Generate "root" class (parent class or base class)
Syntax: class father {

Generate "descendant" (subclass)
Syntax: class son extends father {

Example code of PHP extends class inheritance:

Class father {protected $ name; function _ construct ($ name) {// Constructor $ this-> name = $ name;} function work () {echo "{$ this-> name} I am working;} function _ destruct () {}// destructor} class son extends father {// inherits the parent class function play () {echo "{$ this-> name} I am playing a game ;}} $ my_father = new father ("DAD"); // create a parent class object $ my_father-> work (); $ my_son = new son ("son "); $ my_son-> work (); $ my_son-> play ();

Resolution: in the parent father class, we define general attributes and methods, and then define sub-classes. You may find that there is no constructor or Destructor in the subclass. because the subclass inherits all the methods of the parent class, you can call $ my_son-> work (); this is the inheritance of PHP classes. In addition, you must note that PHP cannot inherit from multiple layers, for example, class A extends B extends C. such inheritance is invalid in PHP. in PHP, there is only one inheritance, and there cannot be more inheritance, other methods are required to "implement" multiple inheritance in disguise.

The above is the content about the encapsulation and inheritance of PHP classes. I hope it will be helpful for everyone.

Member encapsulation encapsulates the member methods and member attributes into the class to hide the details of the attributes and method implementation, through public, protected, private and other restrictions...

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: 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.