Home  >  Article  >  Backend Development  >  How to understand the php construction method

How to understand the php construction method

王林
王林Original
2019-10-11 11:50:442227browse

How to understand the php construction method

What is a constructor method

The constructor method is a special method of a class, its main function is to complete the initialization of new objects.

Features:

1. No return value

2. When creating a new object, the system will automatically call the constructor of the class Complete the initialization of the new diagonal

    语法:
    php5:        修饰符 function __construct()
                        {
                            //code
                        }
    php4:        修饰符 function 类名()
                        {
                            //code
                        }

Note:
1. PHP5 supports both. If both construction methods exist at the same time, the first one is preferred

2. A class has an empty constructor without parameters by default. Once a constructor is customized, the default constructor will be overwritten, so a class has and only one constructor.

3. A class can only have one constructor. (It cannot be overloaded)

4. The default access modifier of the constructor is public.

Recommended tutorial:PHP video tutorial

The above is the detailed content of How to understand the php construction method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn