Analysis of private attribute inheritance in php class

  • 2020-05-26 07:57:16
  • OfStack

Watch this sentence carefully if the parent class has private properties. Then the methods of the parent class only serve the private properties of the parent class.
Let's deepen our understanding with a series of 1 columns.
This example may seem odd, but when you redefine a property $sal in a subclass, the system returns the property of the parent class.
 
<? 
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 is the parent class private attribute . 
return parent::getSal(); 
} 
} 
$manager = new Manager(); 
echo "PHP ".phpversion()."<br>"; 
echo $manager->getSal(); 
echo "<br>"; 
echo "parent's \$sal ".$manager->getParentSal(); 
?> 

Program running results:
 
PHP 5.3.8 
3000 
parent's $sal 3000 

If a property in a parent class is overridden by a subclass. The result is this. Notice that the property definition in line 5 becomes 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(){ 
// This is the parent class private attribute . 
return parent::getSal(); 
} 
} 
$manager = new Manager(); 
echo "PHP ".phpversion()."<br>"; 
echo $manager->getSal(); 
echo "<br>"; 
echo "parent's \$sal ".$manager->getParentSal(); 

?> 

Program running results:
 
PHP 5.3.8 
5000 
parent's $sal 5000 

In the first column, private $sal of the parent class is not overridden, so $manager- > getSal() the parent class method calls the parent class's own private property $sal with two $sal in memory
The parent of the second column's child, protected $sal, has been overwritten by $manager- > The $getSal() superclass method call has been overridden and the $sal of the $sal superclass does not exist in memory with only one $sal in memory
Now let's look at the third column
The method overridden in the subclass is valid for the current private.
 
<? 
class employee{ 
private $sal=3000; 
public function getSal(){ 
return $this->sal; 
} 
} 

class Manager extends employee { 
private $sal=5000; 
// An overwritten method  
public function getSal(){ 
return $this->sal; 
} 
public function getParentSal(){ 
// This is the parent class private attribute . 
return parent::getSal(); 
} 
} 
$manager = new Manager(); 
echo "PHP ".phpversion()."<br>"; 
echo $manager->getSal(); 
echo "<br>"; 
echo "parent's \$sal ".$manager->getParentSal(); 
?> 

The results
 
PHP 5.3.8 
5000 
parent's $sal 3000 

The subclass in this column subclass overrides the getSal() method so it calls the property of the subclass
If you comment this 1 line of the subclass
//private $sal=5000;
You will find 1 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 12-line subclass override method, then echo $manager- > getSal (); The result is a private property of the parent class $sal 3000

Open the zend debug state and see what happens in memory. Notice at the bottom, there are two $sal. Three thousand and five thousand.
 
<? 
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 running results:
 
PHP 5.3.8 
3000 

Change the parent's property $sal to protected, and the subclass overrides the parent's property. Only 1 $sal in 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()."<br>"; 
echo $manager->getSal(); 

?> 

Program running results:
 
PHP 5.3.8 
5000 

If you've learned java, you'll find all of this difficult to understand.
In Java, when subclasses are created, the parent class's properties and methods are created in memory, and even the constructor is called.
This is not the case with PHP5. PHP5 calls the parent class with parent:: instead of parent- > , which is enough to say that PHP5 doesn't want the parent class to be created in memory. PHP5 wants inheritance to be easier than Java.
Just get used to it.

Related articles: