5

我有这个代码:

class A {
  var $arr = array();

  function __construct($para) {
    echo 'Not called';
  }
}

class B extends A {
  function __construct() {
    $arr[] = 'new Item';
  }
}

由于 B 有自己的构造函数,因此 A 永远不会被调用。

现在我可以调用 parent::__construct($para) 但是 B 类需要知道 A 类需要的参数。

我更喜欢这个:

class A {
  var $arr = array();

  function __construct($para) {
    echo 'Not called';
  }
}

class B extends A {
  function __construct() {
    parent::__construct(); // With the parameters class B was created.

    // Additional actions that do not need direct access to the parameters
    $arr[] = 'new Item';
  }
}

这样的东西会起作用吗?

我不喜欢这样一个事实,即所有扩展类 A 的类都需要定义一个新的构造函数,一旦类 A 更改了它的参数,我想要他们做的就是调用类 A 的构造函数,就像当类 B 没有时一样用自己的 __construct() 方法覆盖它。

4

2 回答 2

6

有一种方法可以做到这一点,几乎就像你最初描述的那样,通过使用call_user_func_array()andfunc_get_args()函数:

class B extends A {
    function __construct() {
        // call the parent constructor with whatever parameters were provided
        call_user_func_array(array('parent', '__construct'), func_get_args());

        // Additional actions that do not need direct access to the parameters
        $arr[] = 'new Item';
    }
}

虽然这是一个有趣的练习,但我个人不建议实际使用它——我认为使用单独的init()方法是一个更好的设计。

于 2013-07-24T06:58:48.730 回答
5

一种解决方案是首先不覆盖父构造函数。init()相反,定义父构造函数自动调用的单独(最初为空)方法。然后可以在孩子中覆盖该方法以执行额外的处理。

class A {
    public function __construct($para) {
        // parent processing using $para values

        // ..and then run any extra child initialization
        $this->init();
    }
    protected function init() {
    }
}

class B extends A {
    protected function init() {
        // Additional actions that do not need direct access to the parameters
    }
}
于 2013-07-24T06:50:48.040 回答