Hướng dẫn php multiple constructors - php nhiều hàm tạo

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:

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>

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

class Student {

____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
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
3
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
4
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
5

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

Is

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
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
0
Argument 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
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
3
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4

Argument 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
0
Argument 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
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
3
PHP Fatal error:
Cannot redeclare Student::__construct() in 
/home/33a7c36527d199adf721ab261035d4f7.php on line 10
0
PHP Fatal error:
Cannot redeclare Student::__construct() in 
/home/33a7c36527d199adf721ab261035d4f7.php on line 10
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
4

PHP Fatal error:
Cannot redeclare Student::__construct() in 
/home/33a7c36527d199adf721ab261035d4f7.php on line 10
3

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

class Student {

____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
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
3
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
4
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
5

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
4
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
5
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
6
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
7
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
1
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
9
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
0
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
1

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

Is

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
44

5670

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
9
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
0
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
1

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
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
0
Argument 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
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
3
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
4Student {6

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
36Student {9
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
007Student {2
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
9
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
0
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
1

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

Argument 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
0
Argument 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
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
3
PHP Fatal error:
Cannot redeclare Student::__construct() in 
/home/33a7c36527d199adf721ab261035d4f7.php on line 10
0
PHP Fatal error:
Cannot redeclare Student::__construct() in 
/home/33a7c36527d199adf721ab261035d4f7.php on line 10
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
4

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.

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ố.

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 ().

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
25
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
26
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
7
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
16
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
29

5

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
31

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 ().

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

Đặ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 ().

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 ().

Ví dụ 2:

PHP Fatal error:
Cannot redeclare Student::__construct() in 
/home/33a7c36527d199adf721ab261035d4f7.php on line 10
3

____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
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
0
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
1
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
5

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh

Approach:

  • Isgettype()or data-type specific function like is_int().
  • Is

foo 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
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
11
The following example demonstrates multiple constructors with different data types like string or integer.

PHP

class Student {

____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
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
3
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
4
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
5

Is

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
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
0
Argument 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
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
3
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
19
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
5
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
91
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
5
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
86
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
29

5

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
21
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
97
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
86
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
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
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
0
Argument 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
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
3
PHP Fatal error:
Cannot redeclare Student::__construct() in 
/home/33a7c36527d199adf721ab261035d4f7.php on line 10
0
PHP Fatal error:
Cannot redeclare Student::__construct() in 
/home/33a7c36527d199adf721ab261035d4f7.php on line 10
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
4

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
09
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
10

5

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
21
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
13
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
86
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
1

5

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
4
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
18
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
7
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
0
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
1

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

foo 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
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
29
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
76
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
5

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
21
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
34
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
76
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
1

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo 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
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
42
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
4
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
5

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
21
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
13
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
4
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
1

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

foo 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
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
55

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
4
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
58
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
59
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
21
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
61
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
0
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
1

Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
3
Constructor with 1 parameter called: Akshit
Constructor with 2 parameters called: Akshit,Nikita
Constructor with 3 parameters called: Akshit,Nikita,Ritesh
4
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
66
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
59
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
21
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
69
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
0
Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita
1

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
0
foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

foo Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
bar Object
(
    [firstname] => John
    [lastname] => Doe
    [age] => 25
)
7

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
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
0
Argument 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
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
79
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
75
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
82

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
75
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
84

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
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
0
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
1
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
88

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
85
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
90
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
91
Argument 0 is: John

Argument 0 is: Jane
Argument 1 is: Doe

Argument 0 is: John
Argument 1 is: Doe
Argument 2 is: 25
4

';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
85
';
        for ($i = 0; $i < func_num_args(); $i++) {
            echo 'Argument '.$i.' is: '.$arg_list[$i].'
', "\n"; } } } $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
84

PHP Fatal error:
Cannot redeclare Student::__construct() in 
/home/33a7c36527d199adf721ab261035d4f7.php on line 10
3

Đầu ra

Name is initialized using constructor
ID : 1
Name : Akshit
ID is initialized using constructor
ID : 2
Name : Nikita