Analysis of private attribute inheritance problem in PHP class _php tutorial

Source: Internet
Author: User
Tags class manager
Please read this sentence carefully if the parent class has a private property. Then the parent class's method is only a private property service for the parent class.
Here's a list of the following to deepen your understanding.
This example looks strange, redefining an attribute $sal in a subclass, and the system returns the properties 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 () {
This returns the private property of the parent class.
return Parent::getsal ();
}
}
$manager = new manager ();
echo "PHP". Phpversion (). "
";
echo $manager->getsal ();
echo "
";
echo "Parent ' s \ $sal". $manager->getparentsal ();
?>

Program Run Result:
Copy CodeThe code is as follows:
PHP 5.3.8
3000
Parent ' s $sal 3000

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

?>

Program Run Result:
Copy CodeThe code is as follows:
PHP 5.3.8
5000
Parent ' s $sal 5000

The private $sal of the first Sake parent class is not overridden so $manager->getsal () is called by the parent class's own private property $sal there are two $sal in memory now
The second protected $sal of the sake parent class is overridden $manager->getsal () The parent class's method call has been overridden by the $sal of the $sal parent class that does not exist in memory at this time there is only one $sal in memory
Next, look at the third column.
Methods overridden in subclasses are 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;
The overridden method
Public Function Getsal () {
return $this->sal;
}
Public Function Getparentsal () {
This returns the private property of the parent class.
return Parent::getsal ();
}
}
$manager = new manager ();
echo "PHP". Phpversion (). "
";
echo $manager->getsal ();
echo "
";
echo "Parent ' s \ $sal". $manager->getparentsal ();
?>

Run results
Copy CodeThe code is as follows:
PHP 5.3.8
5000
Parent ' s $sal 3000

This sub-class overrides the Getsal () method so he calls the properties of the subclass.
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 of the 12 line override method then echo $manager->getsal (); The result is a private property of the parent class $sal 3000

Open the Zend debug state to see what is happening in memory. Note the bottom, there are two $sal. are 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 Run Result:
Copy CodeThe code is as follows:
PHP 5.3.8
3000

Changes the parent class's property $sal to protected, and the subclass overrides the parent class's properties. There is only one $sal in 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 Run Result:
Copy CodeThe code is as follows:
PHP 5.3.8
5000

If you've learned Java, you'll find it hard to understand.
In Java, when a subclass is created, the properties and methods of the parent class are created in memory, and even constructors are called.
PHP5 This is not the case, PHP5 calling the parent class is using parent: instead of parent->, which is enough to show that PHP5 does not want the parent class to be created in memory. PHP5 to make inheritance more simple than Java.
Adapt to the good.

http://www.bkjia.com/PHPjc/326161.html www.bkjia.com true http://www.bkjia.com/PHPjc/326161.html techarticle Please read this sentence carefully if the parent class has a private property. Then the parent class's method is only a private property service for the parent class. Here's a list of the following to deepen your understanding. This example looks very odd ...

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