SoFunction
Updated on 2025-03-10

Detailed explanation of the encapsulation and inheritance of PHP classes

Package

Encapsulate member methods and member attributes into the class, hide the details of attributes and method implementation, and limit the access rights of class members such as public, protected, and private. The data is protected internally and can only be operated through authorized member methods, and members can be encapsulated as much as possible.

public:Methods or properties are accessible under any scope and are default, and if no access modifier is specified for a property or method, it will be public.
protected:This class and subclass can be accessed, but external objects cannot be called.
private:It can only be accessed in this class, and it is impossible to call subclasses and external objects. Methods or properties marked by private can be redefined in the inheritance class, and each class can only see private methods it defines itself.

In terms of scope of action, these three modifiers should be sorted from large to small like this: public→protected→private is said to be in scope of action because the class encapsulates some properties and methods, and this encapsulation determines the "visibility" of the data. In this way, we cannot modify the defined properties and methods at will outside the class and can only call them. This is the benefit of encapsulation and also improves security.
We give a code example: 

 class myClass{ 
  public $public="Public";    //public attribute  protected $protected="Protected"; //protected property  private $private="Private";   //private property  function say_Hello() {    //public attribute  //Just give examples and add content by yourself  } 
 
 $obj=new myClass(); 
 echo $obj->public; 
 //echo $obj->protected; 
 //echo $obj->private; 

By running the above example we get a "Public", but when you remove the comment of //echo $obj->private;, you will get the following error:

Fatal error: Cannot access protected property myClass::$protected in E: on line 13。

You can see that we cannot access the property definition of a class at will. We do not know what members there are in this class "outside", because these members may not be impossible for other classes. Of course, if we must access or modify the attributes defined as "private", we can also use the system methods provided by PHP: _get() and _set().

inherit

A class can be made to inherit and have member attributes and methods of another existing class. The inherited class is called the parent class or base class, and the inherited class is a child class. Implement inheritance relationship through extends keyword. In layman's terms, to have inheritance, you must have a "root". You may imagine that if you give birth to a son or daughter in the future, they will get some "things (attributes and methods)" from you, so that your "descendants" will hold all the characteristics of you (roots).

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

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

PHP extends class inherits sample code:

class father{ 
 protected $name; 
 function __construct($name){  //Constructor  $this->name=$name; 
 } 
 
 function work(){ 
  echo "{$this->name}I'm working;
 } 
 function __destruct(){}  //Destructor} 
 
class son extends father{  //Inherit the parent class function play(){ 
  echo "{$this->name}I'm playing a game;
 }  
} 
 
 $my_father=new father(“dad”);  //Create parent class object $my_father->work(); 

 $my_son=new son(“son”); 
 $my_son->work(); 
 $my_son->play(); 

Analysis: In the parent class father, we define general properties and methods, and then define subclasses. You may find that there are no constructors and destructors in the subclass, because the subclass inherits all methods of the parent class, so you can call $my_son->work(); This is the inheritance of the PHP class. Also note: PHP cannot inherit from multiple layers, such as: class A extends B extends C. Such inheritance is invalid in PHP. There is only single inheritance in PHP, and there is no more inheritance. Other methods are needed to "implement" multiple inheritance in disguise.

The above is about the encapsulation and inheritance of PHP classes. I hope it will be helpful to everyone's learning.