PHP构造函数的执行顺序

PHP构造函数的执行顺序

测试代码如下:
<?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
<?php
class grandfather {
    public function __construct(){
        echo 'grandfather';
    }
}
class father extends grandfather {

}
class son extends father {
    
}
$test = new son();
结果:grandfather

由此可见php类的构造函数调用是 从自身向上查找,执行最近的一个,然后stop
原文地址:https://www.cnblogs.com/hongfei/p/2687511.html