Detailed explanation of PHP constructor and destructor

怪我咯
Release: 2023-03-13 16:30:02
Original
2556 people have browsed it

PHPDestructor is the opposite of Constructor. They are called to destroy an object from memory, helping us release the memory occupied by the object properties and destroy the object related H.

The PHP constructor is the first automatically called method after the object is created, and the destructor is the last automatically called method before the object is released. This article introduces you to the PHP constructor and destructor.

php constructor


Constructor is a special method. It is mainly used to initialize the object when creating the object, that is, to assign initial values ​​to the object member variables. It is always used together with the new operator in the statement to create the object. A special class can have multiple constructors, which can be distinguished based on the number of parameters or the types of parameters, that is, the overloading of constructors.

1. It is the "first" "automatically called" method after the object is created.
2. The definition of the construction method , the method name is a fixed ,

In php4: The method with the same name as the class is the constructor method
In php5: The constructor method chooses to use Magic methodconstruct() Declare the constructor in all classes All methods use this name

Advantages: When changing the class name, the constructor does not need to change

Magic method: A certain magic method is written in the class, this The function corresponding to the method will be added.
The method names are all fixed (all provided by the system), and there is no self-defined one.
Each magic method is automatically called at different times to complete a certain function. Methods
Different magic methods have different calling timings
They are all methods starting with
construct(); destruct(); set();......

Function: Initialize member attributes;

php destructor

Destructor (destructor) is the opposite of the constructor, when the object ends its During the life cycle (for example, the function in which the object is located has been called), the system automatically executes the destructor. Destructors are often used to do "clean-up" work (for example, when creating an object, use new to open up a memory space, and delete will automatically call the destructor and release the memory).

1. The last "automatically" called method before the object is released
Use the garbage collector (java php), while c++ releases manually
Function: close some resources, Do some cleaning work

destruct();

php constructor and destructor examples

class Person{ 
var $name; 
var $age; 
var $sex; 
//php4中的构造方法 
/*function Person() 
{ 
//每声明一个对象都会调用 
echo "1111111111111111"; 
}*/ 
//php5中的构造方法 
function construct($name,$age,$sex){ 
$this->name=$name; 
$this->age=$age; 
$this->sex=$sex; 
} 
function say(){ 
//$this->name;//对象中成员的访问使用$this 
echo "我的名字:{$this->name},我的年龄:{$this->age}<br>" 
} 
function run(){ 
} 
function eat(){ 
} 
//析构方法 
function destruct(){ 
} 
} 
$p1=new Person("zhangsan",25,"男"); 
$p2=new Person; 
$p3=new Person;
Copy after login

The above is the detailed content of Detailed explanation of PHP constructor and destructor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!