Home  >  Article  >  Backend Development  >  How to implement virtual method in php

How to implement virtual method in php

藏色散人
藏色散人Original
2020-10-09 09:53:471850browse

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.php

The 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( &#39;Hello&#39; ); 
$b=new AnotherChildClass(); 
$b->say( &#39;Hello&#39; );

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn