Rumah >pembangunan bahagian belakang >masalah PHP >php虚方法怎么实现
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( 'Hello' ); $b=new AnotherChildClass(); $b->say( 'Hello' );
Atas ialah kandungan terperinci php虚方法怎么实现. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!