我看到PHP 5.4的新计划功能包括:traits、数组解引用、JsonSerializable接口以及被称为'closure $this support
'的东西。
http://en.wikipedia.org/wiki/Php#Release_history
虽然其他内容(如JsonSerialiable、数组解引用)要么很清晰,要么我查找了具体信息(traits),但“closure $this support”是什么我不确定。我在谷歌上搜索它或在php.net上找到任何相关信息都没有成功。
有人知道这应该是什么吗?
如果我必须猜测的话,它可能意味着这样:
$a = 10; $b = 'strrrring';
//'old' way, PHP 5.3.x
$myClosure = function($x) use($a,$b)
{
if (strlen($x) <= $a) return $x;
else return $b;
};
//'new' way with closure $this for PHP 5.4
$myNewClosure = function($x) use($a as $lengthCap,$b as $alternative)
{
if(strlen($x) <= $this->lengthCap)) return $x;
else
{
$this->lengthCap++; //lengthcap is incremented for next time around
return $this->alternative;
}
};
这个例子可能微不足道,但其意义在于过去一旦构建了闭包,绑定的“use”变量就会固定。而使用“闭包$this支持”,它们更像是您可以随意操作的成员。
这听起来正确、接近或合理吗?有人知道什么是“闭包$this支持”吗?