Php class method Rewrite principle

Source: Internet
Author: User
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

The final decorated class method cannot be overridden by a quilt class even if the final private method cannot be inherited subclasses cannot be overridden

Class finalmethod{    //can inherit non-overridable    final public Function finalpublic ()    {        echo "Can is inherited, but is Overrided ";    }    Can inherit non-overridable    final protected function finalprotected ()    {        echo "Can is inherited, but being overrided";    }    Non-inheritable cannot be overridden although the subclass inherits the private method of the parent class but also is final restricted cannot override the    final private Function finalprivate ()    {        echo "can Not being inherited or be overrided ";    }    Although not inheritable, this method can be overridden in the subclass    Private Function Private ()    {        echo "can not is inherited, but is overrided";    }} CL Extends finalmethod{//error public    function finalpublic ()    {     }    //error    protected function finalprotected ()    {    }    //error    private Function finalprivate ()    {    }    //correct    public/protected/private function Private ()    {        //subclass Inherits parent Class overriding parent class method when access level can be more relaxed and not more restrictive    }}

2, PHP whether to override the parent class method will only be based on the method name is consistent judgment (5.3 after overriding the parent class method parameter number must be consistent)

This is not to say that the method parameter does not have any effect PHP no overloading mechanism, so it is not necessary to determine whether the rewrite will be overridden only by the method name (c/s) is not only the method name, the parameter is the same as the rewrite, otherwise it is overloaded, that is, the new definition of a polymorphic function state) When the method name is 5.2 Parameters can be different, after 5.3 the parameters must be consistent with the parent class method, and all seek to inherit the access level of the rules.

Class father{public    function Index ($args _1)    {    }} class Child extends father{    // 5.3 After the overriding method must be the same as the parent class, the public    function index ($args _1, $args _2)    {        //in C + +) is overloaded non-overridable, because C + + has a standard polymorphic mechanism, Another state that would be considered a method because of a different parameter        //but in PHP This is still a rewrite but after 5.3 this is illegal must be consistent with the number of method parameters of the parent class    }    //5.3 after the overriding method must be the same    as the parent class persisted parameter number Private Function Index ($args _1, $args _2)    {        //c/c++ is considered 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        // However, PHP will still be treated as an override of the parent class method and will be subject to the elevation rule limit of inherited access    }}

3. When overridden, the access level can be equal to or looser than the parent class cannot elevate the access level

The public method of the parent class cannot be overridden by the quilt class to protected or the Private,protected method cannot be overridden as private, and can be relaxed er, not strictly 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 only be public       //parent class is protected the subclass can be public/protected       // Parent class is private, subclass Public/protected/private can    }}

There are a lot of interesting things about access-level inheritance rules.

Private is impossible to inherit in our common sense, the subclass cannot get it, but its access level is already the highest, so you can write it in subclass private protected public as if we had redefined a function like ourselves, This is particularly prominent before the 5.2 release, since the 5.2 version before the inheritance overrides the parent class method is can not maintain the same number of parameters, but 5.3 strengthens this limitation, the number of parameters must be the same as the parent class

Note:

A subclass that implements an abstract method of a parent class, or a method that implements an interface, is still an inheritance relationship, and still pursues an access level that only lowers the rules that cannot be lifted, and that the method that the abstract method cannot be declared as a private,abstract adornment is definitely for the inheritance implementation. So the method declaration that can only be public or protected interface must be declared in the Public,interface method is also definitely inherited implementation, and can only be public, implements this interface's class also specifies the method of overriding the public type

  • 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.