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

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

1 classFinalmethod2 {3     //inheritable cannot be overridden4     Final  Public functionfinalpublic ()5     {6         Echo"Can be inherited, but being overrided";7     }8     //inheritable cannot be overridden9     Final protected functionfinalprotected ()Ten     { One         Echo"Can be inherited, but being overrided"; A     } -     //non-inheritable cannot be overridden although the subclass inherits the private method of the parent class but also is not overridden by the final limit -     Final Private functionfinalprivate () the     { -         Echo"Can is inherited or be overrided"; -     } -     //Although not inheritable, this method can be overridden in subclasses +     Private function Private() -     { +         Echo"Can is inherited, but is overrided"; A     } at } -  - classOverrideextendsFinalmethod - { -     //Error -      Public functionfinalpublic () in     {  -     } to     //Error +     protected functionfinalprotected () -     { the     } *     //Error $     Private functionfinalprivate ()Panax Notoginseng     { -     } the     //correct +      Public/protected/Private function Private() A     { the         //subclass Inherits Parent Class overriding parent class method when access level can be more lenient and not more stringent +     } -}
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.

1 classFather2 {3      Public functionIndex$args _1)4     {5     }6 }7 8 classChildextendsFather9 {Ten     //5.3 The overriding method must have the same number of arguments as the parent class One      Public functionIndex$args _1,$args _2) A     { -         //This is an overloaded non-override in C + +, 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 a rewrite but after 5.3 this is illegal must be consistent with the number of method parameters of the parent class the     } -     //5.3 The overriding method must have the same number of arguments as the parent class -     Private functionIndex$args _1,$args _2) -     { +         //C + + is considered overloaded because the parameters are different from the parent class method, that is, the state of a function is newly defined, so it is not restricted by inherited access permissions - //But PHP will still be treated as an override of the parent class method and will be restricted by the elevation rule of inherited access rights +     } A}
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

1 classFather2 {3      Public functionindex ()4     {5     }6 }7 8 classChildextendsFather9 {Ten     protected/Private functionindex () One     { A        //Access Elevation Error - //Parent class is public the subclass overrides can also be public only - //Parent class is protected the subclass can be public/protected the //Parent class is private, subclass Public/protected/private can be -     } -}

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, and still pursues an access level that only lowers the rules that cannot be lifted.

And

The method that an abstract method cannot be declared as a private,abstract adornment must be used for an inheritance implementation, so a method declaration that can only be public or protected interface has to be declared in Public,interface and is definitely inherited. And can only be public, the class that implements this interface also specifies a method that overrides the public type

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.