Hướng dẫn what is constructor and destructor with example in php? - Hàm tạo và hàm hủy với ví dụ trong php là gì?

Các hàm tạo là các hàm thành viên đặc biệt cho các cài đặt ban đầu của các trường hợp đối tượng mới được tạo từ một lớp, đây là phần chính của khái niệm hướng đối tượng trong Php5.constructor là các khối xây dựng rất cơ bản xác định đối tượng và bản chất tương lai của nó. Bạn có thể nói rằng các hàm tạo là bản thiết kế để tạo đối tượng cung cấp các giá trị cho các hàm thành viên và biến thành viên. Một đối tượng được khởi tạo, hàm tạo được gọi tự động. Các chất phá hủy là để phá hủy các đối tượng và tự động được gọi vào cuối quá trình thực thi. Trong bài viết này, chúng ta sẽ tìm hiểu về các khái niệm hướng đối tượng của các hàm tạo và người phá hủy. Ngoại trừ các bộ hủy diệt được đi trước bởi một toán tử ~ Tilda.Syntax: & nbsp; & nbsp;PHP5.
Constructors are the very basic building blocks that define the future object and its nature. You can say that the Constructors are the blueprints for object creation providing values for member functions and member variables.
Once the object is initialized, the constructor is automatically called. Destructors are for destroying objects and automatically called at the end of execution.
In this article, we are going to learn about object-oriented concepts of constructors and destructors. 
Both are special member functions of any class with different concepts but the same name except destructors are preceded by a ~ Tilda operator.
Syntax: 
 

  • __construct():   
     
function __construct()
       {
       // initialize the object and its properties by assigning 
       //values
       }
  • __destruct (): & nbsp; & nbsp; 
     
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }

LƯU Ý: Trình xây dựng được xác định trong phần công khai của lớp. Ngay cả các giá trị cho các thuộc tính của lớp cũng được đặt bởi các hàm tạo.Constructor loại: & nbsp; & nbsp; The constructor is defined in the public section of the Class. Even the values to properties of the class are set by Constructors.
Constructor types: 
 

  • Constructor mặc định: Nó không có tham số, nhưng các giá trị cho hàm tạo mặc định có thể được truyền động.It has no parameters, but the values to the default constructor can be passed dynamically.
  • Hàm tạo tham số hóa: Nó lấy các tham số và bạn cũng có thể chuyển các giá trị khác nhau cho các thành viên dữ liệu. It takes the parameters, and also you can pass different values to the data members.
  • Sao chép Constructor: Nó chấp nhận địa chỉ của các đối tượng khác dưới dạng tham số. It accepts the address of the other objects as a parameter.

Kế thừa: Vì sự kế thừa là một khái niệm hướng đối tượng, các hàm tạo được di truyền từ lớp cha sang lớp con có nguồn gốc từ nó. Bất cứ khi nào lớp con có hàm tạo và hàm hủy của riêng chúng, chúng được gọi theo thứ tự ưu tiên hoặc ưu tiên. hàm tạo xác định (__ construct) và hàm tạo do người dùng xác định trong cùng một lớp, hàm tạo được xác định trước trở thành hàm tạo trong khi hàm tạo do người dùng xác định trở thành phương thức bình thường.program: & nbsp; & nbsp; As Inheritance is an object-oriented concept, the Constructors are inherited from parent class to child class derived from it. Whenever the child class has constructor and destructor of their own, these are called in order of priority or preference. 
Pre-defined Default Constructor: By using function __construct(), you can define a constructor.
Note: In the case of Pre-defined Constructor(__construct) and user-defined constructor in the same class, the Pre-defined Constructor becomes Constructor while user-defined constructor becomes the normal method.
Program: 
 

PHP

class Tree

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
2
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
3

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
7
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
8
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
Its a Pre-defined Constructor of the class Tree
1

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
2
Its a Pre-defined Constructor of the class Tree
4

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
7
Its a Pre-defined Constructor of the class Tree
9
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
Its a Pre-defined Constructor of the class Tree
1

Its a Pre-defined Constructor of the class Tree
1

Rakesh : Your position is developer
Vikas : Your position is Manager
4
Rakesh : Your position is developer
Vikas : Your position is Manager
5
Rakesh : Your position is developer
Vikas : Your position is Manager
6
Rakesh : Your position is developer
Vikas : Your position is Manager
7

Rakesh : Your position is developer
Vikas : Your position is Manager
8

Output:  
 

Its a Pre-defined Constructor of the class Tree

Trình xây dựng được tham số hóa: Trình xây dựng của lớp chấp nhận các đối số hoặc tham số. & Nbsp; toán tử -> được sử dụng để đặt giá trị cho các biến. Trong phương thức cấu trúc, bạn có thể gán các giá trị cho các biến trong quá trình tạo đối tượng.program: & nbsp; & nbsp; The constructor of the class accepts arguments or parameters. 
The -> operator is used to set value for the variables. In the constructor method, you can assign values to the variables during object creation.
Program: 
 

PHP

Rakesh : Your position is developer
Vikas : Your position is Manager
9

class Tree

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
Parent class constructor.
Parent class constructor.
Child Class constructor
4
Parent class constructor.
Parent class constructor.
Child Class constructor
5
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
Parent class constructor.
Parent class constructor.
Child Class constructor
4
Parent class constructor.
Parent class constructor.
Child Class constructor
9
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
2
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
3

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
In constructor, destroying Class object! 
1
In constructor, destroying Class object! 
2
Parent class constructor.
Parent class constructor.
Child Class constructor
5
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
In constructor, destroying Class object! 
1
In constructor, destroying Class object! 
7
Parent class constructor.
Parent class constructor.
Child Class constructor
9
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
11

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
7
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
8
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
2
Its a Pre-defined Constructor of the class Tree
4

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
7
Its a Pre-defined Constructor of the class Tree
9
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
Its a Pre-defined Constructor of the class Tree
1

Its a Pre-defined Constructor of the class Tree
1

Rakesh : Your position is developer
Vikas : Your position is Manager
4
Rakesh : Your position is developer
Vikas : Your position is Manager
5
Rakesh : Your position is developer
Vikas : Your position is Manager
6
Rakesh : Your position is developer
Vikas : Your position is Manager
7

Tree4

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
03

Trình xây dựng được tham số hóa: Trình xây dựng của lớp chấp nhận các đối số hoặc tham số. & Nbsp; toán tử -> được sử dụng để đặt giá trị cho các biến. Trong phương thức cấu trúc, bạn có thể gán các giá trị cho các biến trong quá trình tạo đối tượng.program: & nbsp; & nbsp;

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
04
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
03

Rakesh : Your position is developer
Vikas : Your position is Manager
8

Output:  
 

Rakesh : Your position is developer
Vikas : Your position is Manager

class

Parent class constructor.
Parent class constructor.
Child Class constructor
1
Note: If the PHP Class has a constructor, then at the time of object creation, the constructor of the class is called. The constructors have no Return Type, so they do not return anything not even void.
Advantages of using Constructors: 
 

  • function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    1
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    2
    ~ ClassName()
    {
    
    }
    3
    Parent class constructor.
    Parent class constructor.
    Child Class constructor
    5
    ~ ClassName()
    {
    
    }
    5
    Parent class constructor.
    Parent class constructor.
    Child Class constructor
    9
    ~ ClassName()
    {
    
    }
    7
  • function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    1
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    2 4
  • function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    6
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    7
    In constructor, destroying Class object! 
    1class0class1
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    9
  • function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    6
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    7 class5class6
    In constructor, destroying Class object! 
    1class8class9
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    9
  • Tree4
    Rakesh : Your position is developer
    Vikas : Your position is Manager
    5
    Rakesh : Your position is developer
    Vikas : Your position is Manager
    6 Tree7Tree8
    ~ ClassName()
    {
    
    }
    5
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    00
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    01
  • function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    04
    Rakesh : Your position is developer
    Vikas : Your position is Manager
    5
    Rakesh : Your position is developer
    Vikas : Your position is Manager
    6 Tree7
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    08
    ~ ClassName()
    {
    
    }
    5
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    10
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    01

Lưu ý: Phương thức __construct () luôn có yếu tố hiển thị công khai. & Nbsp; chương trình: & nbsp; & nbsp; The __construct() method always have the public visibility factor. 
Program: 
 

PHP

Rakesh : Your position is developer
Vikas : Your position is Manager
9

class

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
17

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
2
Its a Pre-defined Constructor of the class Tree
4

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
25
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
26
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
27
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
Its a Pre-defined Constructor of the class Tree
1

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
Its a Pre-defined Constructor of the class Tree
1

class

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
34
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
35
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
36

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
2
Its a Pre-defined Constructor of the class Tree
4

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
25
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
45

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
25
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
26
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
27
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
Its a Pre-defined Constructor of the class Tree
1

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
Its a Pre-defined Constructor of the class Tree
1

class

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
34
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
35
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
36

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
25
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
26
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
48
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

Rakesh : Your position is developer
Vikas : Your position is Manager
8

Output   
 

Parent class constructor.
Parent class constructor.
Child Class constructor

Rakesh : Your position is developer
Vikas : Your position is Manager
4
Rakesh : Your position is developer
Vikas : Your position is Manager
5
Rakesh : Your position is developer
Vikas : Your position is Manager
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
57
Whenever child class object is created, the constructor of subclass will be automatically called.
Destructor: Destructor is also a special member function which is exactly the reverse of constructor method and is called when an instance of the class is deleted from the memory. Destructors (__destruct ( void): void) are methods which are called when there is no reference to any object of the class or goes out of scope or about to release explicitly. 
They don’t have any types or return value. It is just called before de-allocating memory for an object or during the finish of execution of PHP scripts or as soon as the execution control leaves the block. 
Global objects are destroyed when the full script or code terminates. Cleaning up of resources before memory release or closing of files takes place in the destructor method, whenever they are no longer needed in the code. The automatic destruction of class objects is handled by PHP Garbage Collector.
 

~ ClassName()
{

}

Rakesh : Your position is developer
Vikas : Your position is Manager
4
Rakesh : Your position is developer
Vikas : Your position is Manager
5
Rakesh : Your position is developer
Vikas : Your position is Manager
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
61
The destructor method is called when the PHP code is executed completely by its last line by using PHP exit() or die() functions.

Program:   
 

PHP

Rakesh : Your position is developer
Vikas : Your position is Manager
9

Lưu ý: Bất cứ khi nào đối tượng lớp con được tạo, hàm tạo của lớp con sẽ được gọi tự động.Destructor: Destructor cũng là một hàm thành viên đặc biệt, chính xác là ngược lại phương thức cấu trúc và được gọi khi một thể hiện của lớp bị xóa khỏi bộ nhớ. Destructor (__destruct (void): void) là các phương thức được gọi là khi không có tham chiếu đến bất kỳ đối tượng nào của lớp hoặc đi ra khỏi phạm vi hoặc sắp phát hành rõ ràng. & NBSP; chúng không có bất kỳ loại hoặc giá trị trả về nào. Nó chỉ được gọi trước khi phân bổ bộ nhớ cho một đối tượng hoặc trong khi hoàn thành việc thực thi các tập lệnh PHP hoặc ngay khi điều khiển thực thi rời khỏi khối. & NBSP; Các đối tượng toàn cầu bị phá hủy khi tập lệnh đầy đủ hoặc mã kết thúc. Việc làm sạch tài nguyên trước khi phát hành bộ nhớ hoặc đóng các tệp diễn ra trong phương thức phá hủy, bất cứ khi nào chúng không còn cần thiết trong mã. Việc phá hủy tự động các đối tượng lớp được xử lý bởi người thu gom rác PHP. & NBSP;

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
2
Its a Pre-defined Constructor of the class Tree
4

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
25
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
26
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
27
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
25
In constructor, destroying Class object! 
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
79
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
80
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
Its a Pre-defined Constructor of the class Tree
1

class

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
34
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
35
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
36

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
0

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
25
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
26
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
48
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
Its a Pre-defined Constructor of the class Tree
1

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
1
Its a Pre-defined Constructor of the class Tree
1

Rakesh : Your position is developer
Vikas : Your position is Manager
4
Rakesh : Your position is developer
Vikas : Your position is Manager
5
Rakesh : Your position is developer
Vikas : Your position is Manager
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
57

Rakesh : Your position is developer
Vikas : Your position is Manager
8

Output:   
 

In constructor, destroying Class object! 

Rakesh : Your position is developer
Vikas : Your position is Manager
4
Rakesh : Your position is developer
Vikas : Your position is Manager
5
Rakesh : Your position is developer
Vikas : Your position is Manager
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
61
In the case of inheritance, and if both the child and parent Class have destructors then, the destructor of the derived class is called first, and then the destructor of the parent class. 
 

Lưu ý: Bất cứ khi nào đối tượng lớp con được tạo, hàm tạo của lớp con sẽ được gọi tự động.Destructor: Destructor cũng là một hàm thành viên đặc biệt, chính xác là ngược lại phương thức cấu trúc và được gọi khi một thể hiện của lớp bị xóa khỏi bộ nhớ. Destructor (__destruct (void): void) là các phương thức được gọi là khi không có tham chiếu đến bất kỳ đối tượng nào của lớp hoặc đi ra khỏi phạm vi hoặc sắp phát hành rõ ràng. & NBSP; chúng không có bất kỳ loại hoặc giá trị trả về nào. Nó chỉ được gọi trước khi phân bổ bộ nhớ cho một đối tượng hoặc trong khi hoàn thành việc thực thi các tập lệnh PHP hoặc ngay khi điều khiển thực thi rời khỏi khối. & NBSP; Các đối tượng toàn cầu bị phá hủy khi tập lệnh đầy đủ hoặc mã kết thúc. Việc làm sạch tài nguyên trước khi phát hành bộ nhớ hoặc đóng các tệp diễn ra trong phương thức phá hủy, bất cứ khi nào chúng không còn cần thiết trong mã. Việc phá hủy tự động các đối tượng lớp được xử lý bởi người thu gom rác PHP. & NBSP; 
 

  • Lưu ý: Phương thức phá hủy được gọi khi mã PHP được thực thi hoàn toàn bằng dòng cuối cùng của nó bằng cách sử dụng các hàm thoát php () hoặc chết ().
  • class
    function __destruct() 
           {
           // destroying the object or clean up resources here 
           }
    65

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
25
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
7
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
75
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9
 

function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
6
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
2
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
86
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
25
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
7
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
91
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
92
In constructor, destroying Class object! 
1
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
94class9
function __destruct() 
       {
       // destroying the object or clean up resources here 
       }
9
Rakesh : Your position is developer
Vikas : Your position is Manager
4
Rakesh : Your position is developer
Vikas : Your position is Manager
5
Rakesh : Your position is developer
Vikas : Your position is Manager
6
Its a Pre-defined Constructor of the class Tree
04
Lưu ý: Trong trường hợp thừa kế, và nếu cả hai lớp con và cha mẹ đều có chất phá hủy, thì chất phá hủy của lớp dẫn xuất được gọi là đầu tiên, và sau đó là chất phá hủy của lớp cha. & Nbsp; & nbsp;
Ưu điểm của các kẻ hủy diệt: & nbsp; & nbsp;Destructor tạo cơ hội cho các đối tượng để giải phóng phân bổ bộ nhớ, để có đủ không gian có sẵn cho các đối tượng mới hoặc giải phóng tài nguyên cho các tác vụ khác.
Nó hiệu quả làm cho các chương trình chạy hiệu quả hơn và rất hữu ích khi chúng thực hiện các nhiệm vụ dọn dẹp.So sánh giữa __constructor và __destructor: & nbsp;
Người xây dựngKẻ hủy diệt
Chấp nhận một hoặc nhiều đối số.Không có lập luận được thông qua. Khoảng trống của nó.
Tên hàm là _Construct ().Tên chức năng là _destruct ()
Nó có cùng tên với lớp.Nó có cùng tên với lớp với tiền tố ~ Tilda.
Constructor có liên quan tự động khi đối tượng được tạo.Destructor có liên quan tự động khi đối tượng bị phá hủy.
Được sử dụng để khởi tạo thể hiện của một lớp.Được sử dụng để xác định các đối tượng đã tồn tại để giải phóng bộ nhớ cho chỗ ở mới.
Được sử dụng để khởi tạo các thành viên dữ liệu của lớp.Được sử dụng để làm cho đối tượng thực hiện một số nhiệm vụ trước khi nó bị phá hủy.
Các hàm tạo có thể bị quá tải.
the constructor of base class is created and then the constructor of the derived class.
Destructor không thể bị quá tải.
constructor.
Nó được gọi là mỗi khi một lớp được khởi tạo hoặc đối tượng được tạo.
 
Nó được gọi tự động tại thời điểm xóa đối tượng.

Phân bổ bộ nhớ. In the real programming world, Constructors and Destructor methods are very useful as they make very crucial tasks easier during coding. These encourage re-usability of code without unnecessary repetition. Both of them are implicitly called by compiler even they are not defined in the class.
 


Chất xây dựng và phá hủy trong PHP với ví dụ là gì?

Khi đối tượng được khởi tạo, hàm tạo được tự động gọi.destructor là để phá hủy các đối tượng và tự động được gọi vào cuối quá trình thực thi. Destructors are for destroying objects and automatically called at the end of execution.

Hàm tạo trong PHP với ví dụ là gì?

Trình xây dựng cho phép bạn khởi tạo các thuộc tính của một đối tượng khi tạo đối tượng.Nếu bạn tạo hàm __construct (), PHP sẽ tự động gọi hàm này khi bạn tạo một đối tượng từ một lớp.Lưu ý rằng hàm xây dựng bắt đầu với hai dấu gạch dưới (__)!allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!

Destructor trong PHP với ví dụ là gì?

Một kẻ phá hủy được gọi khi đối tượng bị phá hủy hoặc tập lệnh bị dừng hoặc thoát.Nếu bạn tạo hàm __destruct (), PHP sẽ tự động gọi hàm này ở cuối tập lệnh.Lưu ý rằng chức năng phá hủy bắt đầu với hai dấu gạch dưới (__)!. If you create a __destruct() function, PHP will automatically call this function at the end of the script. Notice that the destruct function starts with two underscores (__)!

Sự khác biệt giữa hàm tạo và chất phá hủy trong PHP là gì?

Constructor được gọi là tự động, trong khi đối tượng được tạo.Destructor được gọi tự động, vì khối được thoát hoặc chương trình chấm dứt.Constructor cho phép một đối tượng khởi tạo một số giá trị của nó trước đó, nó được sử dụng.Destructor cho phép một đối tượng thực thi một số mã tại thời điểm phá hủy của nó. Destructor is called automatically, as block is exited or program terminates. Constructor allows an object to initialize some of its value before, it is used. Destructor allows an object to execute some code at the time of its destruction.