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. even if the final private method cannot be inherited by the subclass, it cannot be aligned 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 does not have an overload mechanism. therefore, to determine whether to rewrite PHP, the method name is used (C/C ++ must not only have the same method name but also the same parameters, otherwise, it is an overload, that is, the state of a newly defined multi-state function)

Class Father {public function index ($ args_1) {}} class Child extends Father {public function index ($ args_1, $ args_2) {// in C/C ++, this is a heavy load non-rewrite, because C/C ++ has a standard polymorphism mechanism, will be treated as another state of a method due to different parameters // but in php this is still a rewrite} private function index ($ args_1, $ args_2) {// C/C ++ is considered as 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 controlled by the access permission }}

3. when rewriting, the access level can only be equal to or relaxed to the parent class and cannot be elevated to the access level. that is, the public method of the parent class cannot be rewritten as protected or private by the quilt class, the protected method cannot be rewritten as private. it can be loose and not strict with er.

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

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.