Home  >  Article  >  Backend Development  >  How is the php object-oriented parent:: keyword used?

How is the php object-oriented parent:: keyword used?

伊谢尔伦
伊谢尔伦Original
2017-07-08 10:04:441988browse

在PHP中不能定义重名的函数,也包括不能再同一个类中定义重名的方法,所以也就没有方法重载

前言

最近在做THINKPHP开发项目中,用到了 parent:: 关键字,实际上 parent::关键字 是PHP中常要用到的一个功能,这不仅仅是在 THINKPHP 项目开发中,即使是一个小型的企业站点,也会有许多相同的function功能,我们可以封装成一个父类 class,然后多个子类直接继承公共部分父类。

PHP5中使用parent::来引用父类的方法。

parent:: 可用于调用父类中定义的成员方法

parent::的追溯不仅于直接父类。

通过parent::调用父类方法

<!-- 声明一个员工类,经理类继承自员工类 --> 
<? 
class employee{ 
 protected $sal=3000;  
 public function getSal(){ 
 $this->sal = $this->sal + 1200; 
 return $this->sal ; 
 } 
} 
 
class Manager extends employee { 
 //如果想让经理在员工工资的基础上多发1500元. 
 //必须先调用父类的getSal()方法. 
 public function getSal(){  
 parent::getSal();// 这里调用了父类的方法. 
 $this->sal = $this->sal + 1500; 
 return $this->sal ; 
 } 
} 
$emp = new employee(); 
echo "普通员工的工资是 " . $emp->getSal(); 
echo "<br>"; 
//xiariboke.com 
$manager = new Manager(); 
echo "经理的工资是: " . $manager->getSal(); 
?>

程序运行结果:

普通员工的工资是 4200

经理的工资是: 5700

父类的private属性

这个东西解释起来十分的不爽。

Private属性是不能被继承的,如果父类有私有的属性。那么父类的方法只为父类的私有属性服务。

下面的例子看起来很奇怪,在子类中重新定义了一个属性$sal,系统却返回了父类的属性。

<?php 
class employee{ 
 private $sal=3000; 
 //protected $sal=3000; 
 public function getSal(){  
 return $this->sal; 
 }  
} 
 
class Manager extends employee { 
 protected $sal=5000; 
 
 public function getParentSal(){ 
 //这里返回的是父类的private属性. 
 return parent::getSal(); 
 } 
} 
$manager = new Manager(); 
echo "PHP ".phpversion()."<br>"; 
echo $manager->getSal(); 
echo "<br>"; 
echo "parent&#39;s \$sal ".$manager->getParentSal(); 
//xiariboke.com 
?>

程序运行结果:

PHP 5.2.9

3000

parent's $sal 3000

如果父类中的属性被子类重写了。结果是这样的。注意 第5行的属性定义变成了protected。

<?php 
class employee{ 
 //private $sal=3000; 
 protected $sal=3000; 
 public function getSal(){  
 return $this->sal; 
 }  
} 
 
class Manager extends employee { 
 protected $sal=5000; 
 //xiariboke.com 
 public function getParentSal(){ 
 //这里返回的是父类的private属性. 
 return parent::getSal(); 
 } 
} 
$manager = new Manager(); 
echo "PHP ".phpversion()."<br>"; 
echo $manager->getSal(); 
echo "<br>"; 
echo "parent&#39;s \$sal ".$manager->getParentSal(); 
 
?>

程序运行结果:

PHP 5.2.9

5000

parent's $sal 5000

子类中重写的方法对当前private有效。

<?php 
class employee{ 
 private $sal=3000; 
 public function getSal(){  
 return $this->sal; 
 } 
} 
 
class Manager extends employee { 
 private $sal=5000; 
 //重写过的方法 
 public function getSal(){  
 return $this->sal; 
 }  
 public function getParentSal(){ 
 //这里返回的是父类的private属性. 
 return parent::getSal(); 
 } 
} 
$manager = new Manager(); 
echo "PHP ".phpversion()."<br>"; 
echo $manager->getSal(); 
echo "<br>"; 
echo "parent&#39;s \$sal ".$manager->getParentSal(); 
 
?>

程序运行结果:

PHP 5.2.9

5000

parent's $sal 3000

打开zend调试状态看看,内存中的情况。注意最下面,有两个$sal 。分别是 3000 和 5000 。

<?php 
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(); 
 
?>

程序运行结果:

PHP 5.2.9

3000

将父类的属性$sal 改成 protected ,子类重写了父类的属性。在内存中只有一个 $sal 。

<?php 
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(); 
 
?>

程序运行结果:

PHP 5.2.9

5000

如果你学过java,你会觉得这一切都是很难理解的。

在Java中当子类被创建时,父类的属性和方法在内存中都被创建,甚至构造函数也要被调用。

PHP5不是这样,PHP5调用父类用的是parent:: 而不是 parent-> ,这足以说明PHP5不想在内存中让父类也被创建。PHP5想让继承变的比Java更简单。

适应下就好。

这样调用会让PHP5.1.1溢出。新版不知道有没有问题。

<?php 
class employee{ 
 private $sal=3000; 
 public function getSal(){  
 return parent::$this->sal; 
 }  
} 
 
class Manager extends employee { 
 protected $sal=5000; 
 public function getSal(){  
 return parent::$this->getSal(); 
 } 
} 
$manager = new Manager(); 
echo "PHP ".phpversion()."<br>"; 
echo $manager->getSal(); 
 
?>

第12行改成这样就好了。注意比较。

return parent:: getSal();

这样的代码引起了递归操作,子类调用父类的方法,父类又调用子类方法。

return parent::$this->getSal();

The above is the detailed content of How is the php object-oriented parent:: keyword used?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn