search
Homephp教程php手册探讨方法的重写(覆载)详解

探讨方法的重写(覆载)详解

Jun 13, 2016 am 11:50 AM
classfunctionphppubliccodecopyDiscussmethodofDetailed explanationrewrite

复制代码 代码如下:


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


在方法覆盖时,务必要使用与原始方法相同的约定,包括参数要一致。属性覆盖也遵循相同的约定。
在覆盖基类的方法后,使用parent关键字仍然可以调用基类的doSomething()方法,而非当前类中的doSomething()方法。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools