Phương thức __construct[]. __construct là một phương thức ma thuật công khai được sử dụng để tạo và khởi tạo một đối tượng lớp. __construct gán một số giá trị thuộc tính trong khi tạo đối tượng. Phương thức này được tự động gọi khi một đối tượng được tạo
Đặc tính
- __construct là một phương pháp ma thuật công cộng
- __construct là một phương thức phải có khả năng hiển thị công khai
- Phương thức __construct có thể chấp nhận một và nhiều đối số
- Phương thức __construct được sử dụng để tạo một đối tượng
- Phương thức __construct có thể gọi phương thức lớp hoặc hàm
- Phương thức __construct cũng có thể gọi hàm tạo của các lớp khác
Hàm tạo sẽ khởi tạo các thuộc tính của lớp tại thời điểm tạo đối tượng. Phương thức __construct[] sẽ chỉ được gọi một lần khi đối tượng của lớp được tạo
cú pháp
$object_name= new class_name [argument value];
Thí dụ
$subject=new computer[“English”];
cú pháp
function __construct[] { // Initialize the object properties }
Cách tiếp cận
- Nhà xây dựng mặc định. Theo mặc định, phương thức __construct[] không có tham số. Các giá trị được truyền cho hàm tạo mặc định là mặc định
- Trình xây dựng được tham số hóa. Trong hàm tạo được tham số hóa, phương thức __construct[] nhận một và nhiều tham số. Bạn có thể cung cấp các giá trị khác nhau cho các tham số
- Sao chép Trình xây dựng. Trong hàm tạo bản sao, phương thức __construct[] chấp nhận địa chỉ của các đối tượng khác làm tham số.
Nhà xây dựng mặc định. Trong hàm tạo mặc định, phương thức __construct[] không có tham số. Các giá trị được truyền cho hàm tạo mặc định là mặc định
Thí dụ. Hãy để chúng tôi lấy ví dụ về một lớp “sinh viên” sẽ hiển thị một thông báo đơn giản cho lớp này, Chúng tôi sẽ định nghĩa một hàm tạo không có tham số.
Constructor không có tham số
PHP
function __construct[] { // Initialize the object properties }44
$subject=new computer[“English”];0
$subject=new computer[“English”];1
$subject=new computer[“English”];2
$subject=new computer[“English”];3
$subject=new computer[“English”];4
$subject=new computer[“English”];5
$subject=new computer[“English”];6
$subject=new computer[“English”];7
$subject=new computer[“English”];8
$subject=new computer[“English”];5
$subject=new computer[“English”];6
function __construct[] { // Initialize the object properties }1____18
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }6
function __construct[] { // Initialize the object properties }7
function __construct[] { // Initialize the object properties }8
function __construct[] { // Initialize the object properties }9
function __construct[] { // Initialize the object properties }30
đầu ra
function __construct[] { // Initialize the object properties }3
Constructor với tham số
ví dụ 2. Chúng ta hãy lấy một ví dụ khác về một lớp “sinh viên” có hai thuộc tính “tên” và “họ“. Đối với lớp này, chúng ta sẽ định nghĩa một hàm tạo với một tham số sẽ khởi tạo các thuộc tính của lớp khi đối tượng được tạo
PHP
function __construct[] { // Initialize the object properties }44
$subject=new computer[“English”];0
function __construct[] { // Initialize the object properties }33
function __construct[] { // Initialize the object properties }34
$subject=new computer[“English”];2____236
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }38
function __construct[] { // Initialize the object properties }39
$subject=new computer[“English”];8
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }38
$subject=new computer[“English”];63
$subject=new computer[“English”];8
$subject=new computer[“English”];2
$subject=new computer[“English”];2____367
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }38
$subject=new computer[“English”];3
$subject=new computer[“English”];41
function __construct[] { // Initialize the object properties }39
$subject=new computer[“English”];43
$subject=new computer[“English”];63
$subject=new computer[“English”];45
$subject=new computer[“English”];5
$subject=new computer[“English”];47____448
function __construct[] { // Initialize the object properties }39
$subject=new computer[“English”];8
$subject=new computer[“English”];5
$subject=new computer[“English”];47____563
$subject=new computer[“English”];63
$subject=new computer[“English”];8
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }4
_______568____569
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }38
$subject=new computer[“English”];3
$subject=new computer[“English”];63
$subject=new computer[“English”];5
$subject=new computer[“English”];65
$subject=new computer[“English”];66
$subject=new computer[“English”];67
$subject=new computer[“English”];47
$subject=new computer[“English”];69
$subject=new computer[“English”];30
$subject=new computer[“English”];67
$subject=new computer[“English”];32
$subject=new computer[“English”];67
$subject=new computer[“English”];47
$subject=new computer[“English”];35
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }4
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }440
function __construct[] { // Initialize the object properties }441
function __construct[] { // Initialize the object properties }7
function __construct[] { // Initialize the object properties }8
function __construct[] { // Initialize the object properties }444
function __construct[] { // Initialize the object properties }445
$subject=new computer[“English”];43
function __construct[] { // Initialize the object properties }447
function __construct[] { // Initialize the object properties }448
function __construct[] { // Initialize the object properties }441
$subject=new computer[“English”];00
function __construct[] { // Initialize the object properties }30
đầu ra
$subject=new computer[“English”];6
Trình hủy PHP. Phương thức PHP Destructor được sử dụng để hủy các đối tượng hoặc giải phóng bộ nhớ thu được của chúng. Hàm hủy được gọi tự động khi đối tượng được tạo. Thông thường, nó được gọi ở cuối tập lệnh. Phương thức hàm hủy không nhận bất kỳ đối số nào, Hàm hủy không trả về bất kỳ kiểu dữ liệu nào. Tất cả quá trình này được xử lý bởi Garbage Collector
Đặc tính
- Phương thức __destroy[] không nhận bất kỳ tham số nào
- Phương thức __destroy[] sẽ không có bất kỳ kiểu trả về nào
- Phương thức này hoạt động hoàn toàn ngược lại với phương thức __construct trong PHP
- __destroy được gọi tự động ở cuối tập lệnh
- Phương thức __destroy[] bắt đầu bằng hai dấu gạch dưới [__]
- Nó được sử dụng để hủy khởi tạo các đối tượng hiện có.
cú pháp
$subject=new computer[“English”];4
Thí dụ
PHP
function __construct[] { // Initialize the object properties }44
$subject=new computer[“English”];0
function __construct[] { // Initialize the object properties }33
________ 105 ________ 13 ________ 14
$subject=new computer[“English”];08
$subject=new computer[“English”];65
$subject=new computer[“English”];10
$subject=new computer[“English”];8
$subject=new computer[“English”];08
$subject=new computer[“English”];65
$subject=new computer[“English”];14
$subject=new computer[“English”];8
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }34
$subject=new computer[“English”];2
$subject=new computer[“English”];3
$subject=new computer[“English”];21
$subject=new computer[“English”];5
$subject=new computer[“English”];65
$subject=new computer[“English”];24
$subject=new computer[“English”];8
$subject=new computer[“English”];5
$subject=new computer[“English”];65
$subject=new computer[“English”];28
$subject=new computer[“English”];8
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }4
$subject=new computer[“English”];33
$subject=new computer[“English”];34
function __construct[] { // Initialize the object properties }7
function __construct[] { // Initialize the object properties }8
$subject=new computer[“English”];37
function __construct[] { // Initialize the object properties }30
đầu ra
$subject=new computer[“English”];6
Thí dụ. Bây giờ, chúng ta hãy lấy một ví dụ về một sinh viên “lớp học” có ba thuộc tính “tên”, “họ” và “trang web yêu thích”. Đối với lớp này, chúng ta sẽ định nghĩa một hàm tạo có tham số và hàm hủy sẽ hủy đối tượng được khởi tạo
PHP
function __construct[] { // Initialize the object properties }44
$subject=new computer[“English”];0
$subject=new computer[“English”];1
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }38
function __construct[] { // Initialize the object properties }39
$subject=new computer[“English”];8
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }38
$subject=new computer[“English”];63
$subject=new computer[“English”];8
$subject=new computer[“English”];2____238
$subject=new computer[“English”];52____18
$subject=new computer[“English”];2____238
$subject=new computer[“English”];3
$subject=new computer[“English”];41
function __construct[] { // Initialize the object properties }39
$subject=new computer[“English”];43
$subject=new computer[“English”];60
$subject=new computer[“English”];63____443
$subject=new computer[“English”];52
$subject=new computer[“English”];45
$subject=new computer[“English”];5
$subject=new computer[“English”];47
$subject=new computer[“English”];67
function __construct[] { // Initialize the object properties }39
$subject=new computer[“English”];8
$subject=new computer[“English”];5
$subject=new computer[“English”];47
$subject=new computer[“English”];72
$subject=new computer[“English”];63
$subject=new computer[“English”];8
$subject=new computer[“English”];5
$subject=new computer[“English”];47
$subject=new computer[“English”];77
$subject=new computer[“English”];52
$subject=new computer[“English”];8
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }34
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }38
$subject=new computer[“English”];3
$subject=new computer[“English”];21
$subject=new computer[“English”];5
$subject=new computer[“English”];65
$subject=new computer[“English”];89
$subject=new computer[“English”];90____667____192
$subject=new computer[“English”];8
$subject=new computer[“English”];5
$subject=new computer[“English”];65
$subject=new computer[“English”];96
$subject=new computer[“English”];90
$subject=new computer[“English”];67____199
$subject=new computer[“English”];8
$subject=new computer[“English”];5
$subject=new computer[“English”];65
function __construct[] { // Initialize the object properties }03
$subject=new computer[“English”];8
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }08
function __construct[] { // Initialize the object properties }7
function __construct[] { // Initialize the object properties }8
function __construct[] { // Initialize the object properties }11
function __construct[] { // Initialize the object properties }12
$subject=new computer[“English”];43
function __construct[] { // Initialize the object properties }14
$subject=new computer[“English”];43
function __construct[] { // Initialize the object properties }16
function __construct[] { // Initialize the object properties }448
function __construct[] { // Initialize the object properties }30
đầu ra
$subject=new computer[“English”];6
Thí dụ. Hãy để chúng tôi tạo một lớp “MyClass”. Trong hàm tạo, chúng ta sẽ định nghĩa một thuộc tính lớp mới và hủy chúng trong hàm hủy.
PHP
function __construct[] { // Initialize the object properties }44
$subject=new computer[“English”];0
function __construct[] { // Initialize the object properties }21
$subject=new computer[“English”];2
$subject=new computer[“English”];3
$subject=new computer[“English”];4
$subject=new computer[“English”];5
$subject=new computer[“English”];65
function __construct[] { // Initialize the object properties }27
$subject=new computer[“English”];8
$subject=new computer[“English”];5
$subject=new computer[“English”];47____448
function __construct[] { // Initialize the object properties }32
$subject=new computer[“English”];8
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }4
$subject=new computer[“English”];2
$subject=new computer[“English”];3
$subject=new computer[“English”];21
$subject=new computer[“English”];5
$subject=new computer[“English”];65
function __construct[] { // Initialize the object properties }41
$subject=new computer[“English”];8
$subject=new computer[“English”];5
$subject=new computer[“English”];6
function __construct[] { // Initialize the object properties }45
$subject=new computer[“English”];67
$subject=new computer[“English”];47
function __construct[] { // Initialize the object properties }48
$subject=new computer[“English”];2
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }4
function __construct[] { // Initialize the object properties }6
function __construct[] { // Initialize the object properties }7
function __construct[] { // Initialize the object properties }8
function __construct[] { // Initialize the object properties }55
function __construct[] { // Initialize the object properties }30
đầu ra
$subject=new computer[“English”];3
Sự kết luận. Trong thế giới thực, các hàm tạo và hàm hủy rất hữu ích vì chúng chiếm nhiều không gian trong bộ nhớ. Chúng cho phép tái sử dụng mã. Nhìn chung chúng rất hữu ích