Bug #66689 [NEW]: Referencing the CLASS constant from an object results in a syntax error
| From: | ramsey@php.net | Date: | Mon, 10 Feb 2014 21:23:37 +0000 |
| Subject: | Bug #66689 [NEW]: Referencing the CLASS constant from an object results in a syntax error | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-184244@lists.php.net to get a copy of this message | ||
From: ramsey
Operating system: Ubuntu 12.04.3 LTS
PHP version: 5.5.9
Package: Class/Object related
Bug Type: Bug
Bug description:Referencing the CLASS constant from an object results in a syntax error
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
--
Edit bug report at https://bugs.php.net/bug.php?id=66689&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66689&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66689&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66689&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=66689&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=66689&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=66689&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=66689&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=66689&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=66689&r=support
Expected behavior: https://bugs.php.net/fix.php?id=66689&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=66689&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=66689&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=66689&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66689&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=66689&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=66689&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=66689&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66689&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=66689&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=66689&r=mysqlcfg