Home Backend Development PHP Tutorial Detailed introduction to PHP's construction method, destructor method and this keyword_PHP Tutorial

Detailed introduction to PHP's construction method, destructor method and this keyword_PHP Tutorial

Jul 13, 2016 am 10:26 AM
this Construction method Destructor method

1. 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. There is no return value.
2. When creating a new object, the system will automatically call the constructor method of the class to complete the initialization of the new object.
Syntax:
php5: Modifier function __construct()

                                                                                                                                                                                                                                                                                through
                                                                                                                                                                       ;

                                                                                                                                                                                                                                                                            through

} <:> Note:
1. PHP5 supports both. If the two constructed methods exist at the same time, choose the first type.

2. In a class Once a constructor with empty parameters is customized, the default constructor will be overridden.


So a class has one and only one constructor.

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

4. The default access modifier of the constructor is public.
2. 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, but can only be used in class definition methods Used in.

3. Example



Copy code

The code is as follows:

header( "Conter-Type:text/html;charset=utf-8"); class Person { public $name; //Member variable
public $age;

// function __construct()
         //{
                                                                                                                                                                                                                 >                   { { ".$this ->age;

}
}
} //new a new object
//$p = new Person();
$p2 = new Person("李思",13);
$p2 ->view();
?>




The result is as follows:
Constructor method with parameters



Copy code

The code is as follows:


Name: Li Si, Age: 13


Four: Destruction method:
Destruction method is a new concept introduced by PHP5. Main function: releasing resources (for example: releasing database links, image resources...).
Syntax:
function __destruct(){}
Features:

1. The destructor method has no return value.

2. The main function is to release resources. It is not to destroy the object itself.
3. Before destroying the object, the system automatically calls the destructor method of the class.

4. A class can have at most one destructor method.

Five: Example:

Copy code The code is as follows:

header("Conter -Type:text/html;charset=utf-8");

class Person
{
public $name;
public $age;
//Construction method
function __construct($name,$age)
{
$this ->name = $name;
age = $age;

}
//Destruction method
function __destruct()
{

";
                                                                                     二",17);
?>




Result: Name: Xiaoer, age 17-->Destroy

Name: Xiaoyi, age 18-->Destroy

Analysis conclusion:

1. The destructor method will be called automatically.

2. The order of calling the destructor method is that the object is created first and then destroyed.

3. When an object has no reference and is confirmed as garbage by the garbage collection mechanism, the destructor method is called.

http://www.bkjia.com/PHPjc/824884.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/824884.htmlTechArticle1. What is a constructor method? A constructor method is a special method of a class. Its main function is to complete the New object initialization. Features: 1. No return value. 2. When creating a new object...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Do php traits support constructors? Do php traits support constructors? Mar 22, 2023 pm 04:54 PM

PHP Traits does not support constructors. Because Traits cannot be instantiated individually, they can only be referenced by classes. Since Trait itself does not have a constructor, it is impossible to define a constructor in Trait.

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

Clever way to use this keyword in jQuery Clever way to use this keyword in jQuery Feb 25, 2024 pm 04:09 PM

Flexible use of this keyword in jQuery In jQuery, the this keyword is a very important and flexible concept. It is used to refer to the DOM element currently being manipulated. By rationally using this keyword, we can easily operate elements on the page and achieve various interactive effects and functions. This article will combine specific code examples to introduce the flexible use of this keyword in jQuery. Simple this example First, let's look at a simple this example. Suppose we have a

What is this? An in-depth analysis of this in JavaScript What is this? An in-depth analysis of this in JavaScript Aug 04, 2022 pm 05:02 PM

What is this? The following article will introduce you to this in JavaScript, and talk about the differences between this in different calling methods of functions. I hope it will be helpful to you!

How to use this method in Java How to use this method in Java Apr 18, 2023 pm 01:58 PM

1. this keyword 1. Type of this: Which object is called is the reference type of that object 2. Usage summary 1. this.data;//Access attribute 2. this.func();//Access method 3.this( );//Call other constructors in this class 3. Explanation of usage 1.this.data is used in member methods. Let us see what will happen if this is not added classMyDate{publicintyear;publicintmonth;publicintday; publicvoidsetDate(intyear,intmonth,intday){ye

How does JavaScript change this pointer? Brief analysis of three methods How does JavaScript change this pointer? Brief analysis of three methods Sep 19, 2022 am 09:57 AM

How does JavaScript change this pointer? The following article will introduce to you three methods of changing this pointer in JS. I hope it will be helpful to you!

Detailed explanation of this in JavaScript arrow function Detailed explanation of this in JavaScript arrow function Jan 25, 2024 pm 01:41 PM

The arrow function in JavaScript is a relatively new syntax. It does not have its own this keyword. On the contrary, the this of the arrow function points to the scope object containing it. The impacts are: 1. This in the arrow function is static; 2. Arrow Functions cannot be used as constructors; 3. Arrow functions cannot be used as methods.

See all articles