测试代码如下:
|
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
|
<?phpclass 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 代码如下:<?phpclass 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
|
<?phpclass grandfather { public function __construct(){ echo 'grandfather'; }}class father extends grandfather { }class son extends father { }$test = new son(); |
结果:grandfather
由此可见php类的构造函数调用是 从自身向上查找,执行最近的一个,然后stop.
本文深入探讨了PHP中类的构造函数调用机制,通过实例代码演示了从自身向上查找并执行最近的构造函数的过程。揭示了类继承与构造函数执行之间的内在联系。
635

被折叠的 条评论
为什么被折叠?



