Home  >  Article  >  Backend Development  >  What does method overriding in php mean?

What does method overriding in php mean?

王林
王林Original
2020-11-04 10:45:013046browse

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.

What does method overriding in php mean?

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,&#39;狗!<br/>&#39;;
    }
}
class zhen extends shao{
    public function aaa()
    {
        echo &#39;猫<br/>&#39;;
    }
}
$lli = new zhen();
$lli->aaa();
$lli->aaa(&#39;binghh&#39;);
?>
<?php
class fu
{
    public function text(){
        echo &#39;父类&#39;;
    }
    public  function  textt(){
        echo &#39;父亲&#39;;
}
}
class erzi extends fu{
    public function text()
    {
        parent::text();//我要父类的东西
        echo &#39;儿子&#39;;//重写

    }
}
$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!

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