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: 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.
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: 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.
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 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.
- 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: 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.
- 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: 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. |