Representation of constructor methods in inheritance in php constructor

Source: Internet
Author: User
This article describes in detail the manifestation of constructor methods in inheritance in php constructor, interested friends can refer to this article to share with you the evolution of php constructor methods in inheritance for your reference. The details are as follows:

When no constructor is defined in a subclass, the constructor of the parent class is automatically called. Therefore, when instantiating a subclass, you must follow the construction method of the parent class.

To:

If a subclass defines its own constructor, the constructor of the parent class is not automatically called, but you can manually call: parent :__ construct ();

However, in subclass, in many cases, the constructor should (required) call the constructor of the parent class to save code and improve readability:

If no destructor is defined in the subclass, the parent class's destructor is automatically called. If a subclass defines its own destructor, the parent class's destructor is not automatically called, but you can manually call: parent :__ destruct (). Override

Rewriting is also called overwriting, which means to re-define the attributes or methods inherited from the parent class-that is, re-write.

But note: The subclass overwrites the method of the parent class. although you can call the method of the same name as the parent class to do some work, it is not necessary. It is also possible that the result of the method executed by the parent class is not suitable for the subclass, and then the subclass is completely written by itself.

Basic requirements for rewriting:

Access control permission: the lower-level access control permission should not be lower than the upper-level access control permission: upper-level: public lower-level: only public upper-level: protected lower-level: protected, public upper-level: private lower-level: private protected public -- this is meaningless. Private ones cannot be covered, but completely regarded as completely new ones.

Method parameter format: it should be consistent with that of the parent class.

Rewriting of private attributes and private methods: Private attributes and methods cannot be overwritten, but subclass can define attributes or methods with the same name as the parent class. It is treated as a new property or method of its own. However, the parameters of the method must be consistent. Constructor rewriting problem: constructor can not only rewrite the constructor like other common methods, but also be looser than normal methods: parameters can be inconsistent during rewriting.

Final class:

Generally, if a class is not specifically declared, "others" can take it for use and "extend" it-inherit.

However:

If you do not want to extend a class, you can declare it as a "final class ".

Form:

Final class name {.... Class definition ....}

Final method

Generally, if a method is not specifically declared, the lower-level class can overwrite it ).

However:

If a method does not want to be overwritten by a lower-level class, its life can be "final method ".

Form:

Final function method name (){.... Method Definition ....}

The above is all the content of this article, hoping to help you learn.

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.