What are the php constructors?

青灯夜游
Release: 2023-02-26 07:52:01
Original
3837 people have browsed it

The php constructor is a special method, mainly used to initialize the object when creating the object; that is, assign initial values to the object member variables, and is always used together with the new operator in the statement to create the object.

What are the php constructors?

When using the new operator to create an instance of a class, the constructor (method) will be automatically called, and its name must be __construct().

Only one constructor can be declared in a class, but the constructor will only be called once every time an object is created. This method cannot be called actively, so it is usually used to perform some useful initialization. Task. This method has no return value.

Grammar:

function __construct(arg1,arg2,...) { ...... }
Copy after login

demo:


        
Copy after login

What are the php constructors?

The above is the detailed content of What are the php constructors?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!