Analysis of private attribute inheritance problem in PHP class _php skill

Source: Internet
Author: User
Tags class manager inheritance php class
Please take a closer look at this sentence if the parent class has a private property. Then the method of the parent class serves only the private property of the parent class.
Here is a series of the following to deepen understanding.
This example looks odd, redefining a property $sal in a subclass, and the system returns the properties of the parent class.
Copy Code code 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 () {
The private property of the parent class is returned here.
return Parent::getsal ();
}
}
$manager = new manager ();
echo "PHP". Phpversion (). " <br> ";
echo $manager->getsal ();
echo "<br>";
echo "Parent ' s \ $sal". $manager->getparentsal ();
?>

Program Run Result:
Copy Code code as follows:

PHP 5.3.8
3000
Parent ' s $sal 3000

If the attributes in the parent class are overridden by the quilt class. The result is this. Note that the property definition for line 5th becomes protected.
Copy Code code 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 () {
The private property of the parent class is returned here.
return Parent::getsal ();
}
}
$manager = new manager ();
echo "PHP". Phpversion (). " <br> ";
echo $manager->getsal ();
echo "<br>";
echo "Parent ' s \ $sal". $manager->getparentsal ();

?>

Program Run Result:
Copy Code code as follows:

PHP 5.3.8
5000
Parent ' s $sal 5000

The private $sal of the first Liezi parent class is not overridden so $manager->getsal () the method of this parent class calls the parent's own private property $sal there are two $sal in memory at this time
The protected $sal of the second Liezi parent class is overridden $manager->getsal () The $sal of the parent class that has been overridden by this parent class is not present in memory when there is only one $sal in memory $sal
Next, look at the third column.
The overridden method in a subclass is valid for the current private.
Copy Code code as follows:

?
Class employee{
Private $sal = 3000;
Public Function Getsal () {
return $this->sal;
}
}

Class Manager extends Employee {
Private $sal = 5000;
Overridden method
Public Function Getsal () {
return $this->sal;
}
Public Function Getparentsal () {
The private property of the parent class is returned here.
return Parent::getsal ();
}
}
$manager = new manager ();
echo "PHP". Phpversion (). " <br> ";
echo $manager->getsal ();
echo "<br>";
echo "Parent ' s \ $sal". $manager->getparentsal ();
?>

Run results
Copy Code code as follows:

PHP 5.3.8
5000
Parent ' s $sal 3000

The neutron class overrides the Getsal () method so he calls the subclass's properties
If you annotate this line of subclasses
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 subclass override method for 12 rows, then echo $manager->getsal (), and the result is the private property of the parent class $sal 3000

Turn on the Zend debug state to see what's in memory. Notice the bottom, there are two $sal. are 3000 and 5000 respectively.
Copy Code code 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 (). " <br> ";
echo $manager->getsal ();
?>

Program Run Result:
Copy Code code as follows:

PHP 5.3.8
3000

Changes the property $sal of the parent class to protected, and the subclass overrides the properties of the parent class. There is only one $sal in memory.
Copy Code code 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 (). " <br> ";
echo $manager->getsal ();

?>

Program Run Result:
Copy Code code as follows:

PHP 5.3.8
5000

If you have studied Java, you will find it difficult to understand.
In Java, when a subclass is created, the properties and methods of the parent class are created in memory, and even the constructor is called.
PHP5 is not the case, PHP5 calls parent: Instead of parent->, this is enough to explain that PHP5 does not want the parent class to be created in memory. PHP5 wants to make inheritance more simple than Java.
Just fit in.

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.