Why does $pri use the properties of the parent class?

Why can the private property $pri? of the parent class be called

<?php
class father {
  public $pub="public";
  private $pri="private";
  protected $pro="protected";
  public function getpri(){
    echo $this->pri;
    echo $this->pro;
    echo $this->pub;
  }
}
class son extends father{
  public $pub="";
  private $pri="";
  protected $pro="";
  public function getpub(){
    echo $this->pub."";
    echo $this->pro;
    echo $this->pri;
  }
}
$son=new son;
$son->getpri();
?>
Php
Sep.27,2021

because only all public and protected methods and properties of the parent class are inherited. Unless the subclass overrides the method of the parent class, the inherited method retains its original function

< hr >

Let's print var_dump ($son);

)

clipboard.png

you will find 2 private pri . Because inheritance inherits only the public and protected methods and properties of the parent class.

also because getpri () is not overwritten, it retains its original function (this method is in father , so $this- > pri accesses private pri of the parent class)

.

attributes declared as public or protected in the parent class can be inherited by the subclass, and changes in the subclass will affect the parent class, as well as changes in the parent class. The properties declared as private in the
parent class are not inherited, but are assigned values to the attributes in the subclass, which have nothing to do with the properties of the parent class, just with the same name. Therefore, changes in the subclass do not affect the parent class.

Menu