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

Bài Viết Liên Quan

Chủ Đề