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.
Features:
1. no return value.
2. when creating a new object, the system automatically calls the constructor of this class to initialize the new diagonal.
Syntax:
Php5: modifier function _ construct ()
{
// Code
}
Php4: modifier function class name ()
{
// Code
}
Note:
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.
Note:
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 ";
/