Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

late static binding in closure PHP5.5 vs 5.6

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.

like image 284
Jarek Tkaczyk Avatar asked Mar 01 '15 08:03

Jarek Tkaczyk


People also ask

What is late static binding?

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.

What is the purpose of late static binding in PHP?

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".

What is late binding and early binding in PHP?

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.

What is dynamic binding in PHP?

Dynamic binding or also known as late binding happens at runtime where class & method binding happens at runtime.


1 Answers

Looks like this bug, fixed in 5.5.14.

like image 84
georg Avatar answered Oct 16 '22 10:10

georg