闭包中的后期静态绑定 PHP5.5 vs 5.6
late static binding in closure PHP5.5 vs 5.6
为什么闭包中的 new static
(在 class static
方法中)等于 PHP5.5 中的 new self
,而它已正确绑定在 PHP5.6 ?
鉴于:
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 中静态绑定更改的提及。
为什么闭包中的 new static
(在 class static
方法中)等于 PHP5.5 中的 new self
,而它已正确绑定在 PHP5.6 ?
鉴于:
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 中静态绑定更改的提及。