PHP5中虚函数的实现方法分享_PHP教程
程序员文章站
2022-04-05 14:09:04
...
请看下面的代码:
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()。
上例的运行结果如下:
A::x() was called. A::y() was called. --
B::x() was called. A::z() was called.
virtual-function.php
class ParentClass {
static public function say( $str ) {
static::do_print( $str );
}
static public function do_print( $str ) {
echo "
}
}
class ChildClass extends ParentClass {
static public function do_print( $str ) {
echo "
}
}
class AnotherChildClass extends ParentClass {
static public function do_print( $str ) {
echo "
}
}
echo phpversion();
$a=new ChildClass();
$a->say( 'Hello' );
$b=new AnotherChildClass();
$b->say( 'Hello' );
复制代码 代码如下:
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()。
上例的运行结果如下:
A::x() was called. A::y() was called. --
B::x() was called. A::z() was called.
virtual-function.php
复制代码 代码如下:
class ParentClass {
static public function say( $str ) {
static::do_print( $str );
}
static public function do_print( $str ) {
echo "
Parent says $str
";}
}
class ChildClass extends ParentClass {
static public function do_print( $str ) {
echo "
Child says $str
";}
}
class AnotherChildClass extends ParentClass {
static public function do_print( $str ) {
echo "
AnotherChild says $str
";}
}
echo phpversion();
$a=new ChildClass();
$a->say( 'Hello' );
$b=new AnotherChildClass();
$b->say( 'Hello' );
上一篇: php如何实现二分查找法
下一篇: php如何禁用eval
推荐阅读
-
PHP实现Javascript中的escape及unescape函数代码分享
-
PHP教程之PHP中shell脚本的使用方法分享
-
PHP实现把文本中的URL转换为链接的auolink()函数分享
-
PHP5中实现多态的两种方法实例分享
-
PHP中CURL方法curl_setopt()函数的参数分享
-
php实现excel中rank函数功能的方法
-
巧用php中的array_filter()函数去掉多维空值的代码分享_PHP教程
-
Laravel框架中扩展函数、扩展自定义类的方法,laravel框架_PHP教程
-
PHP实现Javascript中的escape及unescape函数代码分享,escapeunescape_PHP教程
-
php中的一些数组排序方法分享_PHP教程