php 方法参数传递,在PHP中将实例方法作为参数传递
我想創建一個Listener類
class Listener {
var $listeners = array();
public function add(callable $function) {
$this->listeners[] = $function;
}
public function fire() {
foreach($this->listeners as $function) {
call_user_func($function);
}
}
}
class Foo {
public function __construct($listener) {
$listener->add($this->bar);
}
public function bar() {
echo 'bar';
}
}
$listener = new Listener();
$foo = new Foo($listener);
但是此代碼失敗并出現此錯誤:
Notice: Undefined property: Foo::$bar in index.php on line 18
Catchable fatal error: Argument 1 passed to Listener::add() must be callable, null given, called in index.php on line 18 and defined index.php on line 5
我究竟做錯了什么?
總結
以上是生活随笔為你收集整理的php 方法参数传递,在PHP中将实例方法作为参数传递的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 暗区突围手游红色火药怎么获得
- 下一篇: 不孕症卵泡监测对卵子有影响