php.net |  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: 2021-03-03 11:32 UTC
Votes:10
Avg. Score:4.1 ± 0.9
Reproduced:10 of 10 (100.0%)
Same Version:5 (50.0%)
Same OS:3 (30.0%)
From: ramsey@php.net Assigned: nikic (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.5.9 OS: Ubuntu 12.04.3 LTS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ramsey@php.net
New email:
PHP Version: OS:

 

 [2014-02-10 21:23 UTC] ramsey@php.net
Description:
------------
The "Class Name Resolution As Scalar Via 'class' Keyword" RFC (https://wiki.php.net/rfc/class_name_scalars) 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:
---------------
<?php

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-28 20:48 UTC] nikic@php.net
-Type: Bug +Type: Feature/Change Request
 [2021-03-03 11:32 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2021-03-03 11:32 UTC] nikic@php.net
This has been implemented in PHP 8.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Mar 30 02:01:29 2024 UTC