【发布时间】:2013-09-08 07:11:31
【问题描述】:
我正在尝试使用内置的 array_filter 函数过滤数组。根据 PHP 手册中的示例,我必须提供回调函数的名称。所以,这是我尝试过的:
// This function is a method
public function send($user_id, $access, $content, $aids)
{
// check if user is allowed to see the attachments
function is_owner($var)
{
return $this->attachment_owner($var, $user_id);
}
$aids = implode(';', array_filter(explode(';', $aids), 'is_owner'));
}
这是我得到的错误:
致命错误:在文件名行号中不在对象上下文中时使用 $this。
如何解决?
【问题讨论】:
-
你的 PHP 版本是多少?
-
使用匿名函数:array_filter(..., function($var) { ... }); (抱歉,先没真正理解问题。)
-
$this 不再在范围内。出于好奇,为什么在这里创建一个嵌套函数,而不仅仅是类中的私有函数?
-
“嵌套”函数在全局范围内结束;你不能在里面使用
$this。 -
我有类似的问题,但我不能匿名,因为它被多次使用。而且我不希望它成为一种方法,因为它仅与一种特定方法相关。有什么想法吗?
标签: php