Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php static:: inside anonymous function

Tags:

php

Code on the local machine works good, but strange on the remote server. PHP 5.5.9 on the remote server, PHP 5.5.28 on the local machine what can be the reason of such a strange behaviour on the remote server (PHP version, server configuration, ...) ?

class A {
...
    public static f1(){
        ...
        self::$f2 = static::f2();
        ...
    }
    ...
    protected static function f2() {
        ...
        var_dump(static::class); // returns B on the local machine and on the remote server

        $f3 = function () {
            var_dump(static::class); // returns B on the local machine and returns A on the remote server
            ...
            $data = static::f4() ...
            ...
        };
        ...
    }
...
}
class B extends A{...}
...
self::$B = B::f1();
like image 873
esculpaweb Avatar asked May 14 '26 15:05

esculpaweb


1 Answers

This is a known bug that was fixed in PHP 5.5.14. Below is a quote from the original bug report:

Closures do not correctly capture the late bound class (static::) in some cases

Description:

When a PHP closure is created, it is supposed to capture the late bound class of the enclosing function. There are a number of cases involving derived classes and static methods or static closures (or both) where it doesn't work correctly.

The test script I've included demonstrates the problem. I've tested it on PHP 5.6.0alpha1, but I can also reproduce the problem on various builds of PHP 5.5.

like image 64
helmbert Avatar answered May 16 '26 04:05

helmbert