【发布时间】:2016-01-30 16:47:10
【问题描述】:
我想从 A 类实现一个回调,其中包含特定方法作为参数。例如。
call_user_func_array($callback, ['$this->get', '$this->post']);
但是这不起作用。我的目标是这样做:
index.php
$API = new API();
$API->state('/users', function ($get, $post) {
$get('/', 'UserController.getAll');
});
API.php
public function state ($state, $callback) {
call_user_func_array($callback, ['$this->get', '$this->post']);
}
public method get ($uri, $ctrl) { echo 'getting'; }
public method post ($uri, $ctrl) { echo 'posting'; }
感谢您的任何意见! 我确实意识到使用 $this-> 方法,因为 $this-> 不会在回调范围内存在。
【问题讨论】:
-
我的PHP版本是5.5.12
标签: php class parameters callback