


Detailed introduction to PHP's construction method, destructor method and this keyword_PHP Tutorial
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
;
} <:> Note:
1. PHP5 supports both. If the two constructed methods exist at the same time, choose the first type.
So a class has one and only one constructor.
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:
3. Example
Copy code
The code is as follows:
// 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:
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
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
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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!

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? 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!

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? The following article will introduce to you three methods of changing this pointer in JS. I hope it will be helpful to you!

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.
