相关疑难解决方法(0)

PHP 5.4 - '关闭$ this support'

我看到PHP 5.4的新计划功能是:traits,array dereferencing,JsonSerializable接口和称为' closure $this support'的东西

http://en.wikipedia.org/wiki/Php#Release_history

虽然其他人要么立即清楚(JsonSerialiable,阵列解除引用),要么我查找具体(特征),我不确定'封闭$这个支持'是什么.我在谷歌搜索它或在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;
                     }
                 };
Run Code Online (Sandbox Code Playgroud)

重要性(即使这个例子是微不足道的)是在过去一旦构造了闭包,绑定的"使用"变量是固定的.通过'关闭$ this support',他们更像是你可以搞砸的成员.

这听起来是否正确和/或接近和/或合理?有谁知道这个'封闭$这个支持'是什么意思?

php closures

68
推荐指数
3
解决办法
3万
查看次数

标签 统计

closures ×1

php ×1