测试代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?php class grandfather { public function __construct(){ echo 'grandfather' ; } } class father extends grandfather { public function __construct(){ echo 'father' ; } } class son extends father { public function __construct(){ echo 'son' ; } } $test = new son(); 结果是 : son 代码如下: <?php class grandfather { public function __construct(){ echo 'grandfather' ; } } class father extends grandfather { public function __construct(){ echo 'father' ; } } class son extends father { } $test = new son(); |
结果: father
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php class grandfather { public function __construct(){ echo 'grandfather' ; } } class father extends grandfather { } class son extends father { } $test = new son(); |
结果:grandfather
由此可见php类的构造函数调用是 从自身向上查找,执行最近的一个,然后stop.