Analysis on private property inheritance in php

Source: Internet
Author: User
Tags class manager
First, this question is a bit problematic because the private attribute cannot be inherited. please read this sentence carefully if the parent class has private attributes. The method of the parent class serves only the private attributes of the parent class.
The following describes how to use a series of columns.
This example looks strange. a new attribute $ sal is defined in the subclass, but the system returns the attribute of the parent class.
Copy codeThe code is as follows:
Class employee {
Private $ sal = 3000;
// Protected $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}
Class Manager extends employee {
Protected $ sal = 5000;

Public function getParentSal (){
// Here the private attribute of the parent class is returned.
Return parent: getSal ();
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();
Echo"
";
Echo "parent's \ $ sal". $ manager-> getParentSal ();
?>

Program running result:
Copy codeThe code is as follows:
PHP 5.3.8
3000
Parent'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.
Copy codeThe code is as follows:
Class employee {
// Private $ sal = 3000;
Protected $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}

Class Manager extends employee {
Protected $ sal = 5000;

Public function getParentSal (){
// Here the private attribute of the parent class is returned.
Return parent: getSal ();
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();
Echo"
";
Echo "parent's \ $ sal". $ manager-> getParentSal ();

?>

Program running result:
Copy codeThe code is as follows:
PHP 5.3.8
5000
Parent'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 in the memory.
Protected $ sal of the parent class in the second column is overwritten $ manager-> getSal () the method call of this parent class has been overwritten $ sal of the parent class does not exist in the memory. at this time, there is only one $ sal in the memory.
Next, let's look at the third column.
The method to override a subclass is valid for the current private.
Copy codeThe code is as follows:
Class employee {
Private $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}

Class Manager extends employee {
Private $ sal = 5000;
// Rewrite method
Public function getSal (){
Return $ this-> sal;
}
Public function getParentSal (){
// Here the private attribute of the parent class is returned.
Return parent: getSal ();
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();
Echo"
";
Echo "parent's \ $ sal". $ manager-> getParentSal ();
?>

Running result
Copy codeThe code is as follows:
PHP 5.3.8
5000
Parent's $ sal 3000

The subclass in this column overrides the getSal () method, so it calls the attributes of the subclass.
If you comment out this line 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 12 rows of subclass rewriting methods, 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.
Copy codeThe code is as follows:
Class employee {
Private $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}
Class Manager extends employee {
Protected $ sal = 5000;
Public function getParentSal (){
Return $ this-> sal;
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();
?>

Program running result:
Copy codeThe code is as follows:
PHP 5.3.8
3000

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.
Copy codeThe code is as follows:
Class employee {
Protected $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}
Class Manager extends employee {
Protected $ sal = 5000;
Public function getParentSal (){
Return $ this-> sal;
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();

?>

Program running result:
Copy codeThe code is as follows:
PHP 5.3.8
5000

If you have learned java, you will find it hard to understand.
When a subclass is created in Java, attributes and methods of the parent class are created in the memory, and even constructors are called.
PHP5 does not. 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.
Just adapt.

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.