PHP constructor, destructor, and this keyword

Source: Internet
Author: User
This article introduces PHP constructor, destructor, and this keyword. if you need this keyword, refer to section 1. what is constructor?
Constructor is a special class method. its main function is to complete initialization of new objects.
1. no return value.
2. when creating a new object, the system automatically calls the constructor of this class to initialize the new diagonal.
Php5: modifier function _ construct ()

// Code

Php4: modifier function class name ()

// Code

1. both methods are supported in php5. if both constructor methods exist, the first one is preferred.
2. a class contains a constructor without any parameter. Once a constructor is defined, the default constructor is overwritten.

Therefore, a class has only one constructor.
3. a class can have only one constructor. (reload is not allowed)
4. the default access modifier of the constructor is public.
II. this keyword
This represents the current object. it can be understood as: who calls it, it represents who.
This is not used in class definition and can only be used in class definition methods.
III. instances
The code is as follows:
Header ("Conter-Type: text/html; charset = utf-8 ");
Class Person
Public $ name; // member variable
Public $ age;

// Function _ construct ()
// Echo "constructor without parameters ";


Related Article

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: 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.