PHP method rewriting principle

Source: Internet
Author: User
This knowledge point may be rarely used in our daily work, but I still like to find out some unclear knowledge points.

PHP class method rewriting rules

1. the class method modified by final cannot be overwritten by the quilt class.

The class method modified by final cannot be overwritten by the quilt class. even if the final private method cannot be inherited from the subclass, it cannot be alignment and overwritten.

Class FinalMethod {// it can inherit and cannot override final public function finalPublic () {echo "can be inherited, but be overrided" ;}// it can inherit and cannot override final protected function finalProtected () {echo "can be inherited, but be overrided ";} // It cannot be inherited. it cannot be overwritten. Although the subclass does not inherit the private method of the parent class, it will also be restricted by final. it cannot override final private function finalPrivate () {echo "can not be inherited or be overrided";} // although not inherited, you can override this method in the subclass private 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 () {// when the subclass inherits the parent class and overrides the parent class method, the access level can only be more relaxed, not stricter }}

2. PHP will only judge whether to rewrite the parent class method based on whether the method names are consistent (the number of parameters for rewriting the parent class method after 5.3 must be consistent)

This does not mean that the method parameter has no function. PHP does not have a reload mechanism. therefore, to determine whether to rewrite the method, only the method name is used. (C/C ++ must not only have the same method name, parameters are considered to be overwritten only when they are the same. Otherwise, they are overloaded, that is, the state of a new polymorphism function is defined.) when the method names are the same, they are considered to be rewriting the parent class method, 5.2 The parameters can be different. After 5.3, the parameters must be consistent with the parent class method and follow the rules at the inherited access level.

Class Father {public function index ($ args_1) {}} class Child extends Father {// After 5.3, the override method must be the same as the number of parameters of the parent class. public function index ($ args_1, $ args_2) {// in C/C ++, this is a heavy load non-override, because C/C ++ has a standard polymorphism mechanism, it will be treated as another state of a method due to different parameters. // but in php, this is still rewritten, but after 5.3, this is invalid and must be consistent with the number of method parameters of the parent class.} // The rewriting method after 5.3 must have the same number of parameters as the parent class. private function index ($ args_1, $ args_2) {// C/C ++ is considered as a overload because the parameter is different from the parent class method, that is, the state of a new function is defined, so it will not be restricted by the inherited access permission. // however, php will still be considered as a rewrite of the parent class method and will be subject to the restrictions of the escalation rules for inherited access permissions }}

3. during rewriting, the access level can only be equal to or relaxed to the parent class and cannot be elevated to the access level.

The public method of the parent class cannot be rewritten as protected or private by the quilt class, but the protected method cannot be rewritten as private, and er can be loose or strict

Class Father {public function index () {}} class Child extends Father {protected/private function index () {// access permission escalation error // if the parent class is public, the subclass can only be public. // if the parent class is protected, the subclass can be public/protected. // if the parent class is private, the subclass can be public/protected/private }}

In fact, there are many interesting aspects about access level inheritance rules.

In our common sense, private cannot be inherited, and sub-classes cannot be obtained, but its access level is already the highest, so you can write private protected public in the subclass as if we have redefined a function, which was particularly prominent before version 5.2, because in versions earlier than 5.2, the method for inheriting and rewriting the parent class does not keep the same number of parameters, but in versions later than 5.3, this restriction is enhanced. the number of parameters must be the same as that of the parent class.

Note:

When a subclass implements an abstract method of the parent class or a method that implements an interface, it still belongs to the inheritance relationship. it still pursues access levels and can only reduce the rules that cannot be promoted, and the abstract method cannot be declared as private, abstract modifier methods must be used for inheritance and implementation. Therefore, only public or protected interfaces must be declared as public, and methods declared in interfaces must be inherited and implemented, it can only be public. implements the class of this interface also specifies to override to public type method

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.