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 understand the construction method, destructing method and this keyword of PHP

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

Share

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

This article focuses on "how to understand the construction method of PHP, destructing method and this keyword", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to understand the construction method of PHP, the method of destructing and the this keyword"!

one。 What is the construction method?

Constructor is a special method of class, and its main function is to initialize new objects.

Features:

1. There is no return value.

two。 When creating a new object, the system will automatically call the constructor of this class to complete the initialization of the new diagonal.

Syntax:

Php5: modifier function _ _ construct ()

{

/ / code

}

Php4: modifier function class name ()

{

/ / code

}

Note:

1. Both are supported in php5. If two construction methods exist at the same time, the first one is preferred.

two。 A class has a constructor without empty arguments by default. Once a constructor is customized, the default constructor will be overridden.

So a class has one and only one constructor.

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

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

II. This keyword

This represents the current object. It can be understood as: whoever calls it, it represents.

Note:

This is not used in class definitions and can only be used in methods defined by classes.

three。 Example

The copy code is as follows:

The results are as follows:

Construction method with parameters

The copy code is as follows:

Name: Li Si, age: 13

Fourth, the method of destructing:

Destructing method is a new concept introduced by PHP5. Main role: release resources (for example: release database links, picture resources.).

Syntax:

Function _ _ destruct () {}

Features:

1. The destructor does not return a value.

two。 The main function is to release resources. It is not about destroying the object itself.

3. Before destroying the object, the system automatically calls the destructor of this class.

4. A class has at most one destructor.

Five: examples:

The copy code is as follows:

Results:

Name: waiter, age 17murf-> destroy

Name: junior one, 18 years old-> destroy

At this point, I believe you have a deeper understanding of "how to understand the construction method of PHP, the method of destructing and the this keyword". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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