PHP扩展但使用新的构造函数......可能吗?

Pat*_*ick 3 php

我有一节课:

class test {
    function __construct() {
        print 'hello';
    }
    function func_one() {
        print 'world';
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是有一个类扩展测试类.我说'有点',因为类需要能够运行测试类能够运行的任何函数,但是除非我要求它运行,否则不运行构造.我不想覆盖构造.任何人都知道如何实现这一目标?

Amy*_*y B 5

class test {
    function __construct() {
        print 'hello';
    }
    function func_one() {
        print 'world';
    }
}


class test_2 extends test {
    function __construct() {
        if (i want to) {
            parent::__construct();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @ user177883:在php中你_can_省略了父类的构造函数(只是在派生类的构造函数中没有显式调用它).我不喜欢它,但这就是它的样子. (2认同)