Backend Development 5 min read

Understanding Limited Inheritance in PHP: Public, Protected, Private Members, Static Members, and Constructors

This article explains PHP's limited inheritance concept, detailing how subclasses inherit public and protected members but not private ones, and covers protected inheritance, static members, and the inheritance of constructors and destructors, illustrated with clear code examples.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding Limited Inheritance in PHP: Public, Protected, Private Members, Static Members, and Constructors

In PHP, limited inheritance means that a subclass inherits only selected members of its parent class—specifically public and protected members—while private methods and properties are not inherited.

The following example demonstrates this rule with classes <?php class A{ public $name='张三'; protected $sex='man'; private $age='25'; public function getName(){ echo $this->name."<br>"; } protected function getSex(){ echo $this->sex."<br>"; } private function getAge(){ echo $this->age."<br>"; } } class B extends A{ public function getA(){ $this->getName(); $this->getSex(); $this->getAge(); //父类私有方法无法继承 } } $a = new B(); var_dump($a); /* object(B)#1 (3) { ["name"]=> string(6) "张三" ["sex":protected]=> string(3) "man" ["age":"A":private]=> string(2) "25" } */ ?> shows that public and protected members are accessible in the subclass, while the private method getAge cannot be called.

Protected inheritance works similarly: protected members can be accessed inside the subclass but are not visible from outside code. Attempting to call a protected method from outside results in a fatal error.

To access a parent class's private members, the parent must expose them through public or protected methods; otherwise the private encapsulation defeats inheritance.

Static members, including class constants, also follow inheritance rules. The example below illustrates constants, static properties, and static methods in a parent class Fu and how a child class Zi can use them:

<?php
class Fu{
    const NAME = '人';
    public static $count = 0;
    protected static $type = array('黑','黄','白');

    public static function getCount(){
        echo self::NAME;
        echo self::$count;
    }
    protected static function getType(){
        print_r(self::$type);
    }
}

class Zi extends Fu{
    public static function getFu(){
        Fu::getType();
    }
}

echo Zi::$count; // 允许直接访问
Zi::getCount();   // 访问父类静态方法
Zi::getFu();      // 利用子类公有方法访问父类受保护成员
?>

Constructors ( __construct ) and destructors ( __destruct ) are also inheritable. If a subclass does not define its own constructor, the parent constructor is invoked automatically, and the subclass must supply any required parameters:

<?php
// 父类
class Fu{
    private $money;
    public function __construct($money){
        $this->money = $money;
    }
    public function __destruct(){
        echo 'die';
    }
}

// 子类继承
class Zi extends Fu{}

// 正确实例化,必须传递父类构造所需的参数
$m = new Zi(100);
?>

The article concludes with a link to the original source for further reading.

oopconstructorinheritancestaticprotected
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.