首页 >后端开发 >PHP问题 >php虚方法怎么实现

php虚方法怎么实现

藏色散人
藏色散人原创
2020-10-09 09:53:471931浏览

php虚方法的实现:首先创建一个PHP示例文件;然后通过“d40cdb8e77c4579249701d0225b6afd5x() 的方式调用了 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

代码如下:

<?php 
class ParentClass { 
static public function say( $str ) { 
static::do_print( $str ); 
} 
static public function do_print( $str ) { 
echo "<p>Parent says $str</p>"; 
} 
} 
class ChildClass extends ParentClass { 
static public function do_print( $str ) { 
echo "<p>Child says $str</p>"; 
} 
} 
class AnotherChildClass extends ParentClass { 
static public function do_print( $str ) { 
echo "<p>AnotherChild says $str</p>"; 
} 
} 
echo phpversion(); 
$a=new ChildClass(); 
$a->say( &#39;Hello&#39; ); 
$b=new AnotherChildClass(); 
$b->say( &#39;Hello&#39; );

以上是php虚方法怎么实现的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn