Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the constructor in php

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to write the constructor in php. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

In php, the constructor is a special function in the class, written as "public function _ construct (parameter list) {... code...}"; the parameter list is optional and can be omitted when not needed.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

A constructor (constructor method, also known as a constructor) is a special function in a class that is automatically called when an object is instantiated with the new keyword.

In PHP3.0 and PHP4.0, the constructor is a function with the same name as the class it belongs to. In PHP5, although the usage in PHP3.0 and PHP4.0 is also supported, it is more recommended to use _ _ construct as the constructor of the class, which has the advantage that the constructor does not need to be modified as the class name changes. The usage in PHP3.0 and PHP4.0 is discarded in PHP7.0, and the constructor must be defined with _ _ construct.

A constructor is the first function that is automatically called in a class when an object is created, and only one constructor can exist in a class. Similar to ordinary functions, constructors can also take parameters, and if the constructor has parameters, then the corresponding parameters need to be passed in the instantiation, such as new Students ($name, $age).

The syntax format for creating a constructor is as follows:

Public function _ _ construct (parameter list) {...}

Where the parameter list is optional and can be omitted when it is not needed.

If the constructor is not explicitly declared in the code, there is a constructor with no argument list and empty content in the class by default. If the constructor is explicitly declared, the default constructor in the class will not exist. So constructors are usually used to do some preparatory work, such as assigning values to certain parameters.

Note: if the constructor is explicitly declared, its access must be public, and the constructor is called automatically when instantiated, and we do not need to call it manually.

[example] create a class and create a constructor for its display, as follows:

The running results are as follows:

Https://www.php.cn/ constructor

We use $this in the code, which represents the object currently called, and $this can only be used in the methods of the class.

This is the end of this article on "how to write constructors in php". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report