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
.