Req #66689 [Opn->Csd]: Referencing the CLASS constant from an object results in a syntax error

From: Date: Wed, 03 Mar 2021 11:32:46 +0000
Subject: Req #66689 [Opn->Csd]: Referencing the CLASS constant from an object results in a syntax error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232498@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66689&edit=1 ID: 66689 Updated by: nikic@php.net Reported by: ramsey@php.net Summary: Referencing the CLASS constant from an object results in a syntax error -Status: Open +Status: Closed Type: Feature/Change Request Package: Class/Object related Operating System: Ubuntu 12.04.3 LTS PHP Version: 5.5.9 -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: This has been implemented in PHP 8.0. Previous Comments: ------------------------------------------------------------------------ [2014-02-10 21:23:36] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66689&edit=1

« previous php.bugs (#232498) next »