Questions about rewriting the attributes of the parent class by subclass. Search for confusion

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.