Php inheritance and Private attribute access

class FatherClass
{
    // private $salary = 1000;
    private $salary = 1000;

    public function showInfo()
    {
        echo $this->phone . "<br/>";
        echo $this->salary . "<br/>";
    }
}

class ChildClass extends FatherClass
{
    protected $phone = "13987654321";
    private $salary = 20000;
}

$child = new ChildClass();
$child->showInfo();
echo "
";
print_r($child);

:
13987654321
1000
ChildClass Object
(
    [phone:protected] => 13987654321
    [salary:ChildClass:private] => 20000
    [salary:FatherClass:private] => 1000
)

question: the
child instance now has two private properties. I can understand that the private property calls the
of the class in which the member method is in. Then if you comment out the private $salary in the parent class = 1000; The result will be an error:
Cannot access private property ChildClass::$ salary
cannot access the private properties of the ChildClass class. I can also understand that, because the parent class is outside the class and cannot access the private property
, then the property of protected is also outside the class, how can it be accessed again?

if it is understood that the subclass inherits the member methods of the parent class and therefore accesses the protected property of the subclass, why can"t the private property of the subclass be accessed?

or does it mean that private attributes are classified by regions (as can be seen from the print results). When accessing private properties, only the private properties in this class are accessed, while the properties of public and protected are determined according to the specific value of the object instance?

Thank you. I don"t know if my description is clear

.
May.10,2021

protected can be accessed because protected stipulates that inherited classes can be accessed, but not externally. This keyword is set to do this


The

protected: keyword is that only this class and its subclasses can access
private: only the class itself can access
public: all classes can access

there is no reason, this is the language policy.


Why can't I access the private properties of subclasses

? If you can access it, what is the meaning of the word " private " in the private attribute?


Thank you for your help. Maybe the point I struggle with is not very clear. What I struggle with is how the logic of the parent class member method calling private attributes
now understands: the
subclass overrides the public and protected attributes of the parent class, but not the private attribute of the parent class, so the instance of the subclass now has two private attributes of the same name, and the called member method is in the parent class. So use the private attribute of the parent class
if the parent class does not have the private attribute, then use the subclass, but use the subclass and do not have permission (the member method of the parent class is outside the subclass relative to the subclass), it will prompt
Fatal error: Uncaught Error: Cannot access private property ChildClass::$salary in
if there is no subclass, it will prompt
Notice: Undefined property: ChildClass::$salary in
, which means that the PHP did find the subclass first. If there is, but it is found that there is no permission, so prompt the above Fatal error

= update =
protected class members can be accessed by themselves and their subclasses and parents

Menu