PHP继承问题

Sal*_*ara 1 php oop inheritance class

如果我有以下课程:

class foo {
    function __construct() {
        // Some code
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用继承来创建:

class bar extends foo {
    // Some code
}
Run Code Online (Sandbox Code Playgroud)

当我实例化类'bar'时,它会自动从'foo'执行__construct方法还是我需要做其他事情来让该方法执行?

bca*_*cat 5

手册:

注意:如果子类定义构造函数,则不会隐式调用父构造函数.

虽然文档没有明确说明,但这句话的反转也是正确的,即如果子类没有定义构造函数,则隐式调用父构造函数.因此,在您的示例中,实例化barfoo::__construct自动调用.