php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66811 Cannot access static::class in lambda, writen outside of a class
Submitted: 2014-03-03 05:08 UTC Modified: 2015-05-06 16:17 UTC
Votes:4
Avg. Score:2.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: vovan-ve at yandex dot ru Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.5.9 OS:
Private report: No CVE-ID: None
 [2014-03-03 05:08 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-05-13 11:32 UTC] 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).
 [2015-05-06 15:33 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2015-05-06 16:17 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2015-05-06 16:17 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 10:01:30 2024 UTC