Trả lời cập nhật:
echo '';
// option 1 - combination of both tadeck's and my previous answer
class foo {
function __construct[] {
$arg_names = array['firstname', 'lastname', 'age'];
$arg_list = func_get_args[];
for [$i = 0; $i < func_num_args[]; $i++] {
$this->{$arg_names[$i]} = $arg_list[$i];
}
}
}
$foo = new foo['John', 'Doe', 25];
print_r[$foo];
// option 2 - by default, PHP lets you set arbitrary properties in objects, even
// if their classes don't have that property defined - negating the need for __set[]
// you will need to set properties one by one however, rather than passing them as
// parameters
class bar {
}
$bar = new bar[];
$bar->firstname = 'John';
$bar->lastname = 'Doe';
$bar->age = 25;
print_r[$bar];
Result:
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
Câu trả lời trước:
Result:
Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
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:
- 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.
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
9
Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
0Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
1 Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
2Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
3Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
5 Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
0Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
1 Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
2Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
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
Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
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:
- Nhận tất cả các đối số được gán bằng cách sử dụng PHP func_get_arg [] để nhận giá trị được đề cập từ đối số được truyền dưới dạng các tham số. func_get_arg[] to get a mentioned value from the argument passed as the parameters.
- Trước tiên chúng tôi sẽ đếm số lượng đối số được truyền cho hàm tạo bằng cách sử dụng php func_num_args []. func_num_args[].
- Bằng cách biết loại đối số bằng cách sử dụng chức năng cụ thể loại PHP getType [] hoặc loại dữ liệu như is_int [].gettype[] or data-type specific function like is_int[].
- Đặt tên cho phương thức của bạn theo số lượng đối số và kiểm tra sự tồn tại của nó bằng phương thức php_exists []. method_exists[].
- Gọi hàm phù hợp bằng cách sử dụng call_user_func_array [] bằng cách chuyển các đối số gốc là đối số thứ hai của call_user_func_array [].call_user_func_array[] by passing the original arguments as call_user_func_array[]’s second argument.
Ví dụ 2:
PHP
9
Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
0Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
1 Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
2Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
3Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4Constructor 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
Student {
6Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh3
9 Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
0Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
1 Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
2Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
3Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh3
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
19Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh5
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
91Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh5
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
86foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
29
09
10
13
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
86Name is initialized using constructor ID : 1 Name : Akshit ID is initialized using constructor ID : 2 Name : Nikita1
18
Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh7
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
Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh3
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
7foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
0foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
7foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
0____11 foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
2
29foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
76foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
5Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh3
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
21
34foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
76Name is initialized using constructor ID : 1 Name : Akshit ID is initialized using constructor ID : 2 Name : Nikita1
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
0foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
7____10
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
1 foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
2
42foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
4foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
5Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,Ritesh3
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
21
13foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
4Name is initialized using constructor ID : 1 Name : Akshit ID is initialized using constructor ID : 2 Name : Nikita1
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
0foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
7foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
0____11 foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
2
55Constructor 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
58
59foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
21
61Name 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
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
66
59foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
21
69Name 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
foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
0foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
7foo Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
bar Object
[
[firstname] => John
[lastname] => Doe
[age] => 25
]
7
75 Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
0Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
1 Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
2
79Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4
75
82
75
84
85 Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
0Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
1
88
85
90
91Argument 0 is: John
Argument 0 is: Jane
Argument 1 is: Doe
Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4
85
84PHP Fatal error: Cannot redeclare Student::__construct[] in /home/33a7c36527d199adf721ab261035d4f7.php on line 103
Đầu ra
Name is initialized using constructor ID : 1 Name : Akshit ID is initialized using constructor ID : 2 Name : Nikita