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

What are the constructors and destructors in the class

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "what are the constructors and destructors in the class". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The constructor in the PHP class is also called the constructor. When an object is instantiated with the new keyword, it can be called automatically when the object is created, and it is a special function in the class. The corresponding function is the destructor, which acts as the opposite of the constructor, which can perform operations before the object is destroyed. Next, let's take a look at these two functions.

_ _ construct (): constructor / method

In the class of PHP, we use _ _ construct () as the constructor of the class. The constructor is the first function that is called automatically in the class when the object is created, and there can only be one constructor in a class, and it should be noted that if there are parameters in the constructor, then the instantiation also needs to pass the corresponding parameters.

The syntax format created by the constructor is as follows:

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

It is important to note that the parameter list is optional and can be omitted when not needed. The construct is preceded by two underscores _ _.

Examples are as follows:

The $this in the example represents the currently invoked object. Output result:

From the above result, we call the object created in the class through the _ _ construct () constructor.

_ _ destruct (): destructor / method

The _ _ construct () constructor just mentioned is called when the object is created, corresponding to the destructor, which is the opposite of the constructor. The destructor is called automatically only before the object is deleted from memory. There is a garbage collection mechanism in PHP. When the object cannot be accessed, it automatically starts the garbage collection mechanism, and the destructor is called before the garbage collection object.

The syntax format of the _ _ destruct () function is as follows:

Public function _ _ destruct () {...}

It should be noted that, like constructors, destruct is preceded by two underscores _ _; the difference is that destructors cannot take any arguments.

Examples are as follows:

Output result:

As can be seen from the above example, the time of the constructor is different from that of the destructor. The constructor is called automatically when the object is created, and the destructor is called before the object is collected by the garbage collector.

$this: current object

In PHP object programming, after the object is created, there will be a special object reference "$this" in each member method of the object, which is used in conjunction with the connector-> to complete the access between the internal members of the object. Examples are as follows:

$this-> member attribute; $this-> member method (parameter list)

When we access a member property in a class, we only need to follow the attribute name. There is no need to add a $symbol. $this can only be used in an object. Without an object, there is no $this.

Examples are as follows:

Output result:

This is the end of the content of "what are the constructors and destructors in the class". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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