Class constructor and destructor

Source: Internet
Author: User

There are two methods to create a class:

1. Declare a method name with the same class name in the class (PhP4, not recommended ).

2. Declare a _ construct () method.

Because the constructor is automatically called after the object is created, the constructor is used to initialize the member attributes of the class (assign values );

Class person {

VaR $ name;

VaR $ age;

VaR $ sex;

Function _ construct ($ A = "", $ B = 100, $ c = "male") {// assign an initial value to the parameter variable, because a warning is reported if no value is transferred when the new object is created.

$ This-> name = $;

$ This-> age = $ B;

$ This-> sex = $ C;

}

}

$ P1 = new person ("Kevin", 20, "man ");

$ P2 = new person ("Rose", 18, "female ");

Structure Method:

_ Destruct (); it is automatically called after the script runs. It is generally used to close resources, such as closing database resources and file resources. If the class object or object references such as ($ P1, $ P2) is deleted or released before the entire script is run. The Destructor will be called in advance or called immediately,

 

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.