如何从超类构造函数中调用子类方法?
示例子类:
<?php
include('../classes/A.php');
class B extends A {
public function __construct()
{
parent::__construct($this->view);
}
public function view() {
//something
}
}
$b = new B;
?>
示例超类:
<?php
abstract class A
{
private $callback;
public function __construct($callback)
{
$this->callback = $callback;
call_user_func($this->callback);
}
}
?>
我能做些什么让它有效?
答案 0 :(得分:1)
传递包含对象实例$this
的数组和要调用的方法
(参见调用类方法call_user_func
man page的例子#4)
class B extends A {
public function __construct()
{
parent::__construct(array($this, 'view'));
}
public function view() {
//something
}
}