gpt4 book ai didi

php - 闭包中的后期静态绑定(bind) PHP5.5 vs 5.6

转载 作者:可可西里 更新时间:2023-11-01 13:24:12 26 4
gpt4 key购买 nike

为什么 new static 在闭包中(在类 static 方法中)在 PHP5.5 中等于 new self,而它是正确的在 PHP5.6 中绑定(bind) ?

给定:

abstract class Parent {
public function __construct($something)
{
$this->something = $something;
}

public static function make($array)
{
return array_map(function ($el) {
return new static($el);
}, $array);
}
}

class Child extends Parent {

}

然后

Child::make($someArray); 
// PHP5.5 FatalError: cannot instantiate abstract class Parent
// PHP5.6 works fine, as expected

在 5.5 中,这将按预期工作:

public static function make($array)
{
$child = get_called_class();

return array_map(function ($el) use ($chlid) {
return new $child($el);
}, $array);
}

但为什么会这样呢?我在 php.net 上没有发现任何关于 5.6 中静态绑定(bind)更改的提及。

最佳答案

看起来像this bug , 固定在 5.5.14 .

关于php - 闭包中的后期静态绑定(bind) PHP5.5 vs 5.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28791435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com