php虛擬方法的實作:先建立PHP範例檔;然後透過「e103d4c5a1689fdd6f36a872e8f423d7x() 的方式呼叫了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' );
以上是php虛方法怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!