Method overriding in php means that after a subclass inherits some methods from the parent class, the subclass defines the same method internally, and this newly defined method will override the method inherited from the parent class. , subclasses can only call their internally defined methods, which is method overriding.
Method rewriting:
means that when a subclass inherits some methods of the parent class, the subclass defines the same method internally method, this newly defined method will override the inherited method of the parent class, and the subclass can only call its internally defined methods.
(Recommended tutorial: java video tutorial)
Requirements:
1. When a parent class and a subclass have a method, parameters and name Completely consistent, then the subclass method will override the parent class method.
The parameters must be consistent to achieve method coverage. When the number of parameters is inconsistent, an error will be reported (this involves the overloading of the above-mentioned methods). When the method names are inconsistent, they will not be overwritten, only the newly defined methods of the subclass.
2. When implementing method coverage, the access modifiers can be different, but the access scope of the subclass must be greater than or equal to the access scope of the parent class.
This is the design rule of languages such as php. What I understand is that it is easier to access things at a higher level. If you want to access things at a lower level, you must have higher permissions.
Requires parameters and names to be the same. It is not required that the subclass has the same name as the parent class.
requires that the parameters and names are the same. Specifically, the number of parameters is required to be the same as the parent class, but not the parameter names. That is, the name of the parameters passed can be arbitrary, as long as the number passed is the same.
Example:
<?php class shao { public function sasa($name){ echo $name,'狗!<br/>'; } } class zhen extends shao{ public function aaa() { echo '猫<br/>'; } } $lli = new zhen(); $lli->aaa(); $lli->aaa('binghh'); ?>
<?php class fu { public function text(){ echo '父类'; } public function textt(){ echo '父亲'; } } class erzi extends fu{ public function text() { parent::text();//我要父类的东西 echo '儿子';//重写 } } $shili = new fu(); $erzi = new erzi(); $erzi->text(); ?>
Related recommendations:php training
The above is the detailed content of What does method overriding in php mean?. For more information, please follow other related articles on the PHP Chinese website!