Subclass rewriting of parent class attributes .... Search for confusion

Source: Internet
Author: User
Subclass rewriting of parent class attributes .... Solve the problem of rewriting the parent class attribute of the php subclass

 Sal ;}} class Manager extends Employer {protected $ sal = 5000; public function getParentSal () {return parent: getSal (); // call the parent class method to return the parent class property, why is the 5000} public function getSal () {return $ this-> sal ;}}$ manager = new Manager; echo $ manager-> getParentSal (). PHP_EOL; // 5000 why is it 5000, but the version below 5.3 seems to be 3000 ???? Echo $ manager-> getSal (); // 5000?>


Reply to discussion (solution)

Why not?

5.2.10
5000 5000

5.4.20
5000 5000

Why not?

5.2.10
5000 5000

5.4.20
5000 5000
Can the moderator describe in detail? Why do I use parent: To access the attributes of the parent class? how do I read the attributes of the child class? How is the rewrite method used for rewriting?

Protected is only a protection mode, and will naturally be covered by the quilt class.

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.