Php class method Rewrite principle

Source: Internet
Author: User
Tags php class

Maybe we seldom use this piece of knowledge in our daily work, but I still like to find out the unknown knowledge.

class method rewrite rules for PHP

1. The final modified class method cannot be overridden by a quilt class even if the final private method cannot be inherited the child class cannot be overridden

class finalmethod{    //can be inherited non-overridable     final public  Function finalpublic ()     {        echo  "Can be inherited, but be overrided";    }     //can inherit non-overridable     final protected function finalprotected ()      {        echo  "Can be inherited, but  be overrided ";     }    //not inheritable   Although subclasses do not inherit private methods of the parent class   But at the same time it will be final limit cannot rewrite     final private function finalprivate ()      {        echo  "can not be  Inherited or be overrided ";     }    //Although not inherited   But this method can be overridden in subclasses     PRIVATE&NBsp;function private ()     {        echo  "Can not be inherited ,but be overrided";    }}class  override extends finalmethod{    //error    public  Function finalpublic ()     {     }    // Error    protected function finalprotected ()     {     }    //error    private function  Finalprivate ()     {    }    //correct     public/protected/private function private ()     {    The      //subclass inherits the parent class when overriding the parent class method when the access level is only more lenient   not more restrictive     }}

2, PHP No overload mechanism, so the judgment is not a rewrite only through the method name (c + + is not only the method name is the same, the parameters are the same when the override is considered, otherwise, it is overloaded, that is, the new state of a polymorphic function is defined)

Class father{Public Function index ($args _1) {}}class-extends father{public Function index ($args _1, $a rgs_2) {//C + + This is overloaded non-overriding, because C + + has a standard polymorphic mechanism that can be considered as another state of a method due to different parameters//but in PHP this is still overridden} private functi On index ($args _1, $args _2) {//c/c++ will be overloaded because the parameters are different from the parent class method, i.e. the state of a function is newly defined, so there is no restriction on inherited access rights//But PHP will still be treated as an override of the parent class method, subject to Control of Access Rights}}

3. When overridden, the access level can only be equal to or looser than the parent class, and the access level cannot be elevated, that is, the public method of the parent class cannot be overridden by the quilt class as protected or the Private,protected method cannot be rewritten as private, can be relaxed er, not strict er

Class father{Public Function index () {}}class Child extends father{protected/private function index () { Access Elevation error//Parent class is public the subclass overrides can also be public//parent class protected the subclass can be public/protected//parent class is private subclass publ Ic/protected/private can be}}


Php class method Rewrite principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.