Subclass rewriting of parent class attributes .... Q & lt ;? Php/** & nbsp; * & nbsp; subclass override parent class methods and attributes & nbsp; **/class & nbsp; Employer {protected & nbsp; $ sal = 3000; public & nbs subclass override parent class attributes .... Search for confusion
/**
* Subclass override parent class methods and attributes
**/
Class Employer
{
Protected $ sal = 3000;
Public function getSal ()
{
Return $ this-> sal;
}
}
Class Manager extends Employer
{
Protected $ sal = 5000;
Public function getParentSal ()
{
Return parent: getSal (); // call the parent class method to return the parent class attribute. why is it 5000?
}
Public function getSal ()
{
Return $ this-> sal;
}
}
$ Manager = new Manager;
Echo $ manager-> getParentSal (). PHP_EOL; // 5000 why is this 5000? but the version below 5.3 seems to be 3000 ????
Echo $ manager-> getSal (); // 5000
?>
Questions about rewriting parent class attributes of php subclass:
------ Solution --------------------
Why not?
5.2.10
5000 5000
5.4.20
5000 5000
------ Solution --------------------
Protected is only a protection mode, and will naturally be covered by the quilt class.
------ Solution --------------------
Replace protected with private.