If you have another chance, rewriting override is your way out
Definition : Override, that is, a member with the same name as the parent class is defined in the subclass. The subclass can override any class member of the parent class. Override is usually used to override Methods of the parent class are used to extend or change some business logic.
1. Whether it is a public attribute or a protected attribute
, once overrides
, the parent class
attribute will no longer be exists, and private properties
will not be lost due to overwriting.
<?php class A{ public $name='张三'; protected $sex='man'; private $age='25'; public function getName(){ echo __CLASS__,'<br/>'; echo $this->name."<br>"; } protected function getSex(){ echo __CLASS__,'<br/>'; echo $this->sex."<br>"; } private function getAge(){ echo __CLASS__,'<br/>'; echo $this->age."<br>"; } } class B extends A{ public $name='王五'; protected $sex='woman'; private $age='26'; public function getAll(){ echo $this->name."<br>"; echo $this->sex."<br>"; echo $this->age."<br>"; } } $a=new B(); var_dump($a); /* object(B)#1 (4) { ["name"]=> string(6) "王五" ["sex":protected]=> string(5) "woman" ["age":"B":private]=> string(2) "26" ["age":"A":private]=> string(2) "25" }*/ echo "<br>"; $a->getAll();//王五 woman 26 ?>
It can be found that both public attributes
and protected attributes
are overwritten by , while
private attributes
Because it is not inherited by , it is not affected.
<?php class A{ public $name='张三'; protected $sex='man'; private $age='25'; public function getName(){ echo $this->name."我是父类的getName"."<br>"; } protected function getSex(){ echo $this->sex."我是父类的getSex"."<br>"; } private function getAge(){ echo $this->age."我是父类的getAge"."<br>"; } } class B extends A{ public $name='王五'; protected $sex='woman'; private $age='26'; public function getName(){ echo $this->name."我是子类的getName"."<br>"; } protected function getSex(){ echo $this->sex."我是子类的getSex"."<br>"; } private function getAge(){ echo $this->age."我是子类的getAge"."<br>"; } public function getAll(){ $this->getName(); $this->getSex(); $this->getAge(); } } $a=new B(); $a->getAll();//王五我是子类的getName woman我是子类的getSex 26我是子类的getAge echo "<br>"; ?>
Summary: Overriding public and protected properties directly overrides parent class members, and private properties will not be overwritten; public and protected methods will be rewritten, but private methods will not Overridden (the nature of private methods is not inherited).
2. Subclasses are required to override parent class methods.
a. When a subclass overrides the method of the parent class, the control rights cannot be higher than that of the parent class, that is, the subclass can be more open than the parent class.
<?php class Fu{ protected function show(){ echo __CLASS__,'<br/>'; } } class Zi extends Fu{ protected function show(){} //正确 public function show(){} //允许 private function show(){} //错误:控制权比父类更严格 } ?>
b. Rewriting in PHP requires that when a subclass rewrites a parent class method, it must ensure that the parameters of the method with the same name of the parent class are consistent.
<?php class Fu{ protected function show(){ echo __CLASS__,'<br/>'; } } class Zi extends Fu{ public function show(){} public function show($a){} //错误,与父类同名方法不一致 } ?>
c. Rewriting is for inherited members. The private methods of the parent class will not be inherited, so they are not subject to requirement b.
<?php class Fu{ private function show(){ echo __CLASS__,'<br/>'; } } class Zi extends Fu{ private function show($name){ //不会报错,因为本质不存在重写(父类Fu::show没有被继承) echo $name,'<br/>'; } } ?>
d. Rewriting refers to the special situation of the subclass. Generally, it needs to be extended on the basis of the parent class. At this time, if you want to continue to ensure that the parent class is rewritten The method continues to execute (by default, only new methods overridden by subclasses are always accessed). You need to use the parent
keyword when rewriting methods in subclasses.
<?php class Fu{ protected function show(){ echo __CLASS__,'<br/>'; } } class Zi extends Fu{ public function show(){ parent::show(); //扩展业务逻辑 echo __CLASS__,'<br/>'; } }?>
Summary: parent
cannot access the properties of the parent class, but can access static properties
, static methods
,Class constants
and Common methods
.
Recommended: php tutorial,php video tutorial
The above is the detailed content of If you have another chance, rewriting override is your way out. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.
