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. Show
function __construct() { // initialize the object and its properties by assigning //values }
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.
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. PHP
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 Tree1 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 Tree4 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 Tree9 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 Tree1 Its a Pre-defined Constructor of the class Tree1 Rakesh : Your position is developer Vikas : Your position is Manager4 Rakesh : Your position is developer Vikas : Your position is Manager5 Rakesh : Your position is developer Vikas : Your position is Manager6 Rakesh : Your position is developer Vikas : Your position is Manager7 Rakesh : Your position is developer Vikas : Your position is Manager8 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. PHPRakesh : Your position is developer Vikas : Your position is Manager9
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 constructor4 Parent class constructor. Parent class constructor. Child Class constructor5 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 constructor4 Parent class constructor. Parent class constructor. Child Class constructor9 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 constructor5 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 constructor9 function __destruct() { // destroying the object or clean up resources here }9 function __destruct() { // destroying the object or clean up resources here }1 1 |