Hướng dẫn php multiple constructors - php nhiều hàm tạo
Trả lời cập nhật:
Result:
Câu trả lời trước:
Result:
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. & NBSP; Trong PHP, một hàm tạo là một phương thức có tên __construct (), được gọi bởi từ khóa mới sau khi tạo đối tượng. Các hàm tạo cũng có thể chấp nhận các đối số, trong trường hợp đó, khi câu lệnh mới được viết, bạn cũng cần gửi các đối số cấu trúc cho các tham số.__construct(), which is called by the keyword new after creating the object. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor arguments for the parameters. Các hàm tạo là các khối xây dựng rất cơ bản xác định đối tượng trong tương lai và bản chất 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. Các loại constructor:
Lưu ý: PHP thiếu hỗ trợ để khai báo nhiều hàm tạo của các số tham số khác nhau cho một lớp không giống như các ngôn ngữ như Java.PHP lacks support for declaring multiple constructors of different numbers of parameters for a class unlike languages such as Java. Nhiều hàm tạo: Nhiều hơn một hàm tạo trong một lớp duy nhất để khởi tạo các trường hợp có sẵn. More than one constructor in a single class for initializing instances are available. Ví dụ 1: Trong ví dụ sau, chúng tôi tạo một tập lệnh và cố gắng khai báo nhiều hàm tạo. Chúng tôi sẽ gán tên của một học sinh bằng một hàm tạo và tuổi với một hàm tạo khác. In the following example, we create a script and try to declare multiple constructors. We will assign the name of a student using one constructor and age with another constructor. PHP
____10 1 2 3 4 5 0 7Is 0 7 7 9 0 1 2 3 4 5 0 1 2 3PHP Fatal error: Cannot redeclare Student::__construct() in /home/33a7c36527d199adf721ab261035d4f7.php on line 100 PHP Fatal error: Cannot redeclare Student::__construct() in /home/33a7c36527d199adf721ab261035d4f7.php on line 101 4PHP Fatal error: Cannot redeclare Student::__construct() in /home/33a7c36527d199adf721ab261035d4f7.php on line 103 Output: PHP Fatal error: Cannot redeclare Student::__construct() in /home/33a7c36527d199adf721ab261035d4f7.php on line 10 Chúng tôi phải sử dụng các phương pháp khác nhau để sử dụng nhiều hàm tạo trong PHP. Một số được liệt kê dưới đây. Approach:
Ví dụ 2: PHP
____10 1 2 3 4 5Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh3 Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh4 Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh5 Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh6 Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh7 Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh1 Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh9 Name is initialized using constructor ID : 1 Name : Akshit ID is initialized using constructor ID : 2 Name : Nikita0 Name is initialized using constructor ID : 1 Name : Akshit ID is initialized using constructor ID : 2 Name : Nikita1 0 7Is Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh3 Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh4 4 |