PHP类继承间的方法调用关系
PHP和C++一样允许类的继承,但存在一些自有的特性:
1. 仅允许继承一个类,或者继承多个接口;
2. 默认类中的所有方法都具有C++中虚函数的特性,可以被子类重新覆盖;除非使用final关键字,此时子类不可重新覆盖该方法。
在类中通常使用$this->... 来访问类的属性或者方法,此时$this实际上是实例化对象的映射,因此$this是不可能出现在静态方法中的,这一点与C++很类似。除此之外,还能在类中使用self::... 和parent::... 来分别调用自身类方法和父类的方法,这些是PHP中特有的关键字。
接下来需要解答具体的问题了。
1. 如何在子类中调用自身的方法?
答:使用$this->... 或者 self::... 来调用即可,但在静态方法中只能使用self::... ,而且需要被调用方法中也没有使用到$this关键字。
2. 如何在子类中调用父类的方法?
答:无论是子类未覆盖的方法,直接使用$this->... 即可调用父类方法;如果是子类覆盖了的方法,需要使用到parent::.. 来调用。
3. 如何在父类中调用子类的方法?
答:如之前所述,默认类中所有方法都具有C++中虚函数的特性,因此如果父类中使用$this->... 调用某方法,无论该方法是否在父类中存在,而该方法被子类定义了,那么实际上调用的就是子类的方法。
具体见以下代码:
class A { public function x() { echo "A::x() was called.\n"; } public function y() { self::x(); echo "A::y() was called.\n"; } public function z() { $this->x(); echo "A::z() was called.\n"; } } class B extends A { public function x() { echo "B::x() was called.\n"; } } $b = new B(); $b->y(); echo "--\n"; $b->z();
该例中,A::y()调用了A::x(),而B::x()覆盖了A::x(),那么当调用B::y()时,B::y()应该调用A::x()还是B::x()呢?在C++中,如果A::x()未被定义为虚函数,那么B::y()也就是A::y())将调用A::x(),而如果A::x()使用virtual关键字定义成虚函数,那么B::y()将调用B::x()。
然而,在PHP5中,虚函数的功能是由 self 和 $this 关键字实现的。如果父类中A::y()中使用 self::x() 的方式调用了 A::x(),那么在子类中不论A::x()是否被覆盖,A::y()调用的都是A::x();而如果父类中A::y()使用 $this->x() 的方式调用了 A::x(),那么如果在子类中A::x()被B::x()覆盖,A::y()将会调用B::x()。
上例的运行结果如下:
--
B::x() was called. A::z() was called.
<?php class pp { //p的父类 public function __construct(){ echo "pp __construct<br>"; } } class p extends pp { //继承pp protected $aa = '1'; protected $bb = '2'; protected $cc = '3'; public function __construct(){ echo "p __construct<br>"; } public function draw(){ self::a(); echo $this->aa."<br>"; echo $this->bb."<br>"; } public function init() { parent::__construct(); } public function a() { echo $this->cc."<br>"; } } class c extends p { //子类继承p protected $cc = 500000; function __construct(){ $this->init(); echo "c __construct<br>"; } public function a() { echo $this->cc."111a<br>"; } } $obj = new c; $obj->draw(); ?>
pp __construct
c __construct
500000
1
2
$this->a();
echo $this->aa."<br>";
echo $this->bb."<br>";
}
public function init() {
parent::__construct();
}
private function a() {
echo $this->cc."<br>";
}
c __construct
500000
1
2
echo $this->cc."<br>";
}
c __construct
500000111a
1
2
<?php class pp { public function __construct(){ echo "pp __construct<br>"; } } class p extends pp { protected $aa = '1'; protected $bb = '2'; protected $cc = '3'; public function __construct(){ echo "p __construct<br>"; } public function draw(){ $this->a(); echo $this->aa."<br>"; echo $this->bb."<br>"; } public function init() { parent::__construct(); } } class c extends p { protected $cc = 500000; function __construct(){ $this->init(); echo "c __construct<br>"; } private function a() { echo $this->cc."111a<br>"; } } $obj = new c; $obj->draw(); ?>
c __construct
- 下一篇: UNIX 高手的 10 个习惯
- 上一篇: 问题驱动式的学习方式的优劣
相关推荐
- PHP time()返回的结果统一的,不同的是date()的结果
- Posted on 02月20日
- PHP修改crontab
- Posted on 07月02日
- sphinx 增量索引 实现近实时更新
- Posted on 02月25日
- 所需Web服务器台数预估方法
- Posted on 07月12日