How to call the constructor for creating class instances in php _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php creates a class instance's constructor call problem. This article describes how to construct a method call when php creates a class instance. Unlike java, php automatically calls the parent class when creating a class instance, constructor call problems.

Unlike java, php automatically first calls the constructor of the parent class (Superclass) when creating a class instance to ensure that all attributes can be correctly initialized. Php will not automatically call the constructor of the parent class in the constructor of this class. If you really need to call the constructor of the parent class, you can manually call parent ::__ construct ($ params ...);

Class Base {
Function _ construct (){
// Do something here ....
}
}

If we need some Extender to inherit the Base, we need to note that

1. do we need to call the constructor of the parent class for initialization?

2. do we need to perform other extension operations on the basis of the parent class constructor?

3. we do not need the initialization actions in the Base constructor.

If our Extender only meets Scenario 1, we can omit the constructor of Extender. because the constructor of the parent class fully complies with our requirements, we do not need to perform additional encoding.

If our Extender meets scenario 2, we need to call parent ::__ construct () in the Extender constructor, initialize it first, and then perform some extension operations.

If our Extender meets scenario 3, we can choose not to call the constructor of the parent class. There is a defect in doing this: when we expand extended Extender, if this extension class requires initialization of the Base constructor, we call parent ::__ construct () again () only the constructor of Extender can be called, but the constructor of Base class cannot be called.

Therefore, I personally think it is best to manually call the constructor of the parent class in the constructor of the subclass.

Bytes. Unlike java, php automatically calls the parent class (Super...

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.