Detailed explanation of rewriting (overriding) of discussion methods_PHP tutorial

WBOY
Release: 2016-07-21 15:08:05
Original
742 people have browsed it

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...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!