Home > Backend Development > PHP Tutorial > How to implement virtual functions in php

How to implement virtual functions in php

怪我咯
Release: 2023-03-13 21:48:02
Original
4208 people have browsed it

VirtualFunctionIn order to meet the needs of overloading and polymorphism, it is defined in the base class. Even if the definition is empty, it can be rewritten or not in the subclass. Write functions in base classes!

Pure virtual functions are not defined in the base class and must be implemented in the subclass, much like the interface function in Java!

Virtual function

Reason for introduction: In order to facilitate the use of polymorphic features, we often need to define virtual functions in the base class.

How to implement this virtual function in php5? Please look at the following code:

<?php 
class A { 
public function x() { 
echo "A::x() was called.\n"; 
} 
public function y() { 
self::x(); 
echo "A::y() was called.\n"; 
} 
public function z() { 
$this->x(); 
echo "A::z() was called.\n"; 
} 
} 
class B extends A { 
public function x() { 
echo "B::x() was called.\n"; 
} 
} 
$b = new B(); 
$b->y(); 
echo "--\n"; 
$b->z(); 
?>
Copy after login

In this example, A::y() calls A::x(), and B::x() covers A:: x(), then when calling B::y(), should B::y() call A::x() or B::x()? In C++, if A::x() is not defined as a virtual function, then B::y() (that is, A::y()) will call A::x(), and if A::x () is defined as a virtual function using the virtual keyword, then B::y() will call B::x(). However, in PHP5, the functionality of virtual functions is implemented by the self and $this keywords. If A::y() in the parent class calls A::x() using self::x(), then in the subclass no matter whether A::x() is overridden or not, A::y( ) all call A::x(); and if A::y() in the parent class calls A::x() using $this->x(), 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.
Copy after login

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; );
Copy after login

The above is the detailed content of How to implement virtual functions in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template