Why new static
in the closure (in a class static
method) is equal to new self
in PHP5.5, while it's properly bound in PHP5.6 ?
Given:
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 {
}
then
Child::make($someArray);
// PHP5.5 FatalError: cannot instantiate abstract class Parent
// PHP5.6 works fine, as expected
In 5.5 this will work as expected:
public static function make($array)
{
$child = get_called_class();
return array_map(function ($el) use ($chlid) {
return new $child($el);
}, $array);
}
but why is this happening? I haven't found any mentions on php.net concerning static binding changes in 5.6.
Whenever a PHP interpreter gets the request to compile a function. If it sees any static property, then it leaves the property pending for run time and the property gets its value during runtime from the function it is being called. This is called late static binding.
PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance. More precisely, late static bindings work by storing the class named in the last "non-forwarding call".
If the compiler knows at the compile-time which function is called, it is called early binding. If a compiler does not know at compile-time which functions to call up until the run-time, it is called late binding.
Dynamic binding or also known as late binding happens at runtime where class & method binding happens at runtime.
Looks like this bug, fixed in 5.5.14.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With