|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66689 Referencing the CLASS constant from an object results in a syntax error
Submitted: 2014-02-10 21:23 UTC Modified: 2015-08-28 20:48 UTC
Avg. Score:4.1 ± 0.9
Reproduced:10 of 10 (100.0%)
Same Version:5 (50.0%)
Same OS:3 (30.0%)
From: Assigned:
Status: Open Package: Class/Object related
PHP Version: 5.5.9 OS: Ubuntu 12.04.3 LTS
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-02-10 21:23 UTC]
The "Class Name Resolution As Scalar Via 'class' Keyword" RFC ( introduced the new "class" alias that may be used to get the fully-qualified name of a class. It does not specify how this alias may be used from the context of an instance variable, but since it looks like a class constant, one assumes that it should be treated like a class constant.

In the given test script, the BAZ constant may be accessed from the class name as Bar::BAZ or from the instance variable as $f::BAZ.

Likewise, the "class" alias may be accessed as Bar::CLASS, but it results in a syntax error when attempting to access it from the instance variable as $f::CLASS.

The solution is to use get_class($f), but I consider this a bug. Since the "class" alias looks like a built-in class constant, it makes sense to treat like a full class constant and access it from an instance variable.

Test script:

namespace Foo;

class Bar
    const BAZ = 'hello';

$f = new Bar;

var_dump(Bar::CLASS);  // dumps "Foo\Bar"
var_dump($f::BAZ);     // dumps "hello"
var_dump($f::CLASS);   // Results in syntax error

Expected result:
string(7) "Foo\Bar"
string(5) "hello"
string(7) "Foo\Bar"

Actual result:
PHP Parse error:  syntax error, unexpected 'CLASS' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /home/ramsey/test.php on line 14


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-28 20:48 UTC]
-Type: Bug +Type: Feature/Change Request
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Jan 22 12:13:32 2021 UTC