Bug #66811 [Asn->Csd]: Cannot access static::class in lambda, writen outside of a class

From: Date: Wed, 06 May 2015 16:17:30 +0000
Subject: Bug #66811 [Asn->Csd]: Cannot access static::class in lambda, writen outside of a class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192521@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66811&edit=1

 ID:                 66811
 Updated by:         nikic@php.net
 Reported by:        vovan-ve at yandex dot ru
 Summary:            Cannot access static::class in lambda, writen
                     outside of a class
-Status:             Assigned
+Status:             Closed
 Type:               Bug
 Package:            Scripting Engine problem
 PHP Version:        5.5.9
 Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

Fixed in PHP 7 by https://github.com/php/php-src/commit/ab97606b8a9501599cf027b13b72fc0c92e3a16e.
This won't be backported, as it depends on a bunch of other changes, some ABI incompatible.


Previous Comments:
------------------------------------------------------------------------
[2014-05-13 11:32:06] vovan-ve at yandex dot ru

PHP 5.3 allow us to use $object::CONSTANT, but $object::class doesn't work in the same manner,
which would be better than get_class($object).

------------------------------------------------------------------------
[2014-03-03 05:08:08] vovan-ve at yandex dot ru

Description:
------------
Since PHP 5.4 a closure can be bound to a object of a class to work like a method.
Since PHP 5.5 class name can be fetched with special syntax SomeClass::class, like a pseudo-constant
with name 'class'.

This second feature does not work in a lambda, writen outside of a class. It cause syntax error.

Test script in a sandbox: http://3v4l.org/XO0bZ

May be, related with #66622 ?

Test script:
---------------
<?php
class A {
    const BAZ = 37;

    public static function f() {
        return static function () {
            var_dump(static::BAZ);
            var_dump(static::class);
        };
    }
}

$f = A::f();
$f();

$g = $f->bindTo(null, A::class);
$g();

$foo = static function () {
    var_dump(static::BAZ);
    var_dump(static::class);    // line 21 is here
};

$bar = $foo->bindTo(null, A::class);
$bar();

Expected result:
----------------
int(37)
string(1) "A"
int(37)
string(1) "A"
int(37)
string(1) "A"


Actual result:
--------------
Fatal error: Cannot access static::class when no class scope is active in ... on line 21


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=66811&edit=1


Thread (3 messages)

« previous php.bugs (#192521) next »