Home >Backend Development >PHP Tutorial >Detailed explanation of rewriting (overriding) of discussion methods_PHP tutorial

Detailed explanation of rewriting (overriding) of discussion methods_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:08:05777browse

Copy code The code is as follows:

class Cart{
public function Cart(){
echo "Calling Cart()
";
}
public function doSomething(){
echo "Calling doSomethimg()
";
}
}
class Named_Cart extends Cart{
function Named_Cart(){
echo "Calling Named_Cart()
";
}
function doSomething() {
echo "Calling Named_Cart::doSomething()
";
}
}
$myCart=new Cart();
$myCart->doSomething ();
$myNamed_Cart=new Named_Cart();
$myNamed_Cart->doSomething();
?>

When overriding the method, be sure to use The same conventions as the original method, including the parameters, must be consistent. Property overrides follow the same convention.
After overriding the method of the base class, you can still call the doSomething() method of the base class using the parent keyword instead of the doSomething() method in the current class.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327489.htmlTechArticleCopy the code as follows: ?php class Cart{ public function Cart(){ echo "Calling Cart()br /"; } public function doSomething(){ echo "Calling doSomethimg()br /"; } } class...
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