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: 2015-08-28 20:48 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:
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] 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
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Jul 21 00:01:26 2019 UTC