Understanding and transformation of PhP5 Constructor

Source: Internet
Author: User

From http://www.ximenyifan.com

"

In PhP4, the constructor of a class is a function with the same name by default.

PhP5 not only follows this naming method, but also introduces some new elements:

_ Construct () is a method name, which is the default constructor in PhP5

What are the advantages of this function? First, when you change the class name, its constructor no longer needs to change with the class name.

Second

    1. Class onE {
    2. Function onE {
    3. $ This-> test ();
    4. }
    5. }
    6. Class Two extends onE {
    7. Function test {
    8. Echo & quot; 123 & quot ";
    9. }
    10. }
    11. $ Test = new two ();

CopyCode

This is an incorrect PHP 4 syntax.Class E is inherited and executed simultaneously onThe construction of E is still, the test method is called at this time, but at this time the test is not onE.

    1. Class onE {
    2. Function _ construct (){
    3. $ This-> test ();
    4. }
    5. }
    6. Class Two extends onE {
    7. Function test {
    8. Echo & quot; 123 & quot ";
    9. }
    10. }
    11. $ Test = new two ();

Copy code

In PhP5, this problem was solved through _ construct.

At the same time, parent ::__ construct () distinguishes between the constructor of the parent class and the Child class.

PhP5 not only has _ construct () but also has _ destruct ()

We call it destructor:

The constructor is opposite to the constructor. The Destructor is newly added to PhP5 and does not contain the destructor in PhP4. The Destructor allows you to perform operations or complete some functions before destroying a class, such as closing a file and releasing a result set, the Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed, that is, the Destructor is called before the object is destroyed in the memory. Similar to the constructor name, The Destructor name of a class must be _ destruct ()

PHP also has interfaces. interfaces are defined as interface classes.

The PHP method also distinguishes private public and so on. Generally, the default value is public.

"

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.