Explanation of private property inheritance of php classes

Source: Internet
Author: User
Tags class manager
Explanation of private property inheritance of php classes

  1. Class employee {

  2. Private $ sal = 3000;
  3. // Protected $ sal = 3000;
  4. Public function getSal (){
  5. Return $ this-> sal;
  6. }
  7. }
  8. Class Manager extends employee {
  9. Protected $ sal = 5000;

  10. Public function getParentSal (){

  11. // Here the private attribute of the parent class is returned.
  12. Return parent: getSal ();
  13. }
  14. }
  15. $ Manager = new Manager ();
  16. Echo "PHP". phpversion ()."
    ";
  17. Echo $ manager-> getSal ();
  18. Echo"
    ";
  19. Echo "parent's \ $ sal". $ manager-> getParentSal ();
  20. ?>

Program running result: PHP 5.3.83000parent's $ sal 3000

If the attribute quilt class in the parent class is overwritten. The result is as follows. Note that the attribute definition of row 3 is changed to protected.

  1. Class employee {

  2. // Private $ sal = 3000;
  3. Protected $ sal = 3000;
  4. Public function getSal (){
  5. Return $ this-> sal;
  6. }
  7. }

  8. Class Manager extends employee {

  9. Protected $ sal = 5000;

  10. Public function getParentSal (){

  11. // Here the private attribute of the parent class is returned.
  12. Return parent: getSal ();
  13. }
  14. }
  15. $ Manager = new Manager ();
  16. Echo "PHP". phpversion ()."
    ";
  17. Echo $ manager-> getSal ();
  18. Echo"
    ";
  19. Echo "parent's \ $ sal". $ manager-> getParentSal ();
  20. ?>

Program running result: PHP 5.3.85000parent's $ sal 5000

The private $ sal of the parent class in the first column is not overwritten, so $ manager-> getSal () the method of this parent class calls the private attribute of the parent class $ sal. at this time, there are two $ sal protected of the parent class in the second column in the memory. $ manager-> getSal () the method of this parent class calls the $ sal parent class that has been overwritten. $ sal does not exist in the memory. at this time, there is only one $ sal in the memory. Next, let's look at the method that is rewritten in the third column sub-class. valid for the current private.

  1. Class employee {

  2. Private $ sal = 3000;
  3. Public function getSal (){
  4. Return $ this-> sal;
  5. }
  6. }

  7. Class Manager extends employee {

  8. Private $ sal = 5000;
  9. // Rewrite method
  10. Public function getSal (){
  11. Return $ this-> sal;
  12. }
  13. Public function getParentSal (){
  14. // Here the private attribute of the parent class is returned.
  15. Return parent: getSal ();
  16. }
  17. }
  18. $ Manager = new Manager ();
  19. Echo "PHP". phpversion ()."
    ";
  20. Echo $ manager-> getSal ();
  21. Echo"
    ";
  22. Echo "parent's \ $ sal". $ manager-> getParentSal ();
  23. ?>

Running result PHP 5.3.85000parent's $ sal 3000

The subclass in this column overrides the getSal () method, so it calls the attributes of the subclass. if you comment on this row of the subclass/private $ sal = 5000, you will find an error: Notice: undefined property: Manager ::$ sal in E: \ wamp \ www \ oo \ 2-5 \ 2-5-3.php on line 14. if you comment out the rewrite method for the subclass of 12 rows, echo $ manager-> getSal (); the result is the private attribute of the parent class $ sal 3000.

Open the zend debugging status to check the memory. Note that there are two $ sal at the bottom. 3000 and 5000 respectively.

  1. Class employee {
  2. Private $ sal = 3000;
  3. Public function getSal (){
  4. Return $ this-> sal;
  5. }
  6. }
  7. Class Manager extends employee {
  8. Protected $ sal = 5000;
  9. Public function getParentSal (){
  10. Return $ this-> sal;
  11. }
  12. }
  13. $ Manager = new Manager ();
  14. Echo "PHP". phpversion ()."
    ";
  15. Echo $ manager-> getSal ();
  16. ?>

Program running result: PHP 5.3.83000

Change the attribute $ sal of the parent class to protected, and the subclass overrides the attribute of the parent class. There is only one $ sal in the memory.

  1. Class employee {
  2. Protected $ sal = 3000;
  3. Public function getSal (){
  4. Return $ this-> sal;
  5. }
  6. }
  7. Class Manager extends employee {
  8. Protected $ sal = 5000;
  9. Public function getParentSal (){
  10. Return $ this-> sal;
  11. }
  12. }
  13. $ Manager = new Manager ();
  14. Echo "PHP". phpversion ()."
    ";
  15. Echo $ manager-> getSal ();
  16. ?>

Program running result: PHP 5.3.85000Note:: PHP5 uses parent: instead of parent-> to call the parent class, which means PHP5 does not want to create the parent class in the memory. PHP5 wants to make inheritance easier than Java.

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.