如何为子线程编写一个需要在父线程工作时运行的构造函数


How to wite a constructor for child that need to run when parent is working?

我有一个父类对所有活动进行日志记录工作。现在我想使用一个子类向一些活动添加额外的数据。这意味着我需要父类关于活动的变量,并且我需要在父类被调用时运行子类。

这是父类的构造函数:

function CD_Log( $id = false ) {
    $this->__construct( $id );
}
function __construct( $id = false ) {
    if ( !empty( $id ) ) {
        $this->id = $id;
        $this->populate();
    }
}
我认为子构造函数应该是这样的:
function __construct( $args = array() ) {
    parent::__construct($child_id);
    $defaults = array(
    'child_id'    => 0, //the child has its own id
    'child_data'  => 0,  //extra data
    'parent_data' => $this->data // inheritage from parent
    );
}

在调用子元素的函数中,我将提供默认的数据数组。子进程将在父进程被调用的相同位置被调用。这段代码可以工作吗?

这个问题我已经读了好几遍了,发现它令人难以置信地困惑。我真的很想帮你,但我认为如果你能更明确地说明你想要什么以及系统应该做什么,会更容易。

为什么在运行子类时必须运行父类?你能试着把这些信息委托给孩子吗?另外,当构造函数在CD_Log方法之前初始化时,为什么要通过另一个方法调用构造函数?