为什么我不能在 PHP 5.5.4 中使用 $this 作为词法变量?
2022-08-30 11:38:30
$ php --version
PHP 5.5.4 (cli) (built: Sep 19 2013 17:10:06)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
以下代码(类似于 https://bugs.php.net/bug.php?id=49543 的示例):
class Foo
{
public function bar()
{
return function() use ($this)
{
echo "in closure\n";
};
}
}
失败,出现:
PHP Fatal error: Cannot use $this as lexical variable
然而,根据PHP文档和Rasmus Lerdorf对该错误报告的评论,在匿名函数中使用$this是从PHP 5.4开始添加的。我做错了什么?