jcu*_*bic 10 php closures this anonymous-function
我有这样的功能:
class Service {
function delete_user($username) {
...
$sessions = $this->config->sessions;
$this->config->sessions = array_filter($sessions, function($session) use ($this){
return $this->get_username($session->token) != $username;
});
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为你不能$this在里面使用use,是否有可能在回调中执行作为类Service成员的函数?或者我需要使用或foreach循环?
Shi*_*ai7 24
$this自PHP 5.4以来,(非静态)闭包中始终可用,不需要use它.
class Service {
function delete_user($username) {
...
$sessions = $this->config->sessions;
$this->config->sessions = array_filter($sessions, function($session) {
return $this->get_username($session->token) != $username;
});
}
}
Run Code Online (Sandbox Code Playgroud)