Home >Backend Development >PHP Problem >How to implement virtual method in php
Php virtual method implementation: First create a PHP sample file; then pass "d713d4031b425214c45e53425e7a3f69x(), then if A::y() in the subclass ::x() is overridden by B::x(), A::y() will call B::x().
The running results of the above example are as follows:
A::x() was called. A::y() was called. -- B::x() was called. A::z() was called. virtual-function.phpThe code is as follows:
<?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' );
The above is the detailed content of How to implement virtual method in php. For more information, please follow other related articles on the PHP Chinese website!