|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25816 Class constants of type array hold directly inaccessible keys/values
Submitted: 2003-10-10 00:24 UTC Modified: 2004-01-09 08:58 UTC
From: dan at wep dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2003-11-29 OS: *
Private report: No CVE-ID: None
 [2003-10-10 00:24 UTC] dan at wep dot net
Constants defined inside classes can be defined as an array prepopulated with key, value pairs; however this data is not directly accessible.

It is possible to create a local variable copy of the constant at runtime and use it to access the data.

If class constants are not meant to be able to hold array data, then a parse error should of been thrown on the 'const' declaration.

Reproduce code:
class test {
  const someData = array('foo' => 'bar');

  function __construct() {
    $dataCopy = someData;
    print($dataCopy['foo']);  // This works
    print(var_dump(someData));  // This works (shows all array information)
    print(someData['foo']);  // This throws a parse error


$obj = new test();

Expected result:

.. Jackpot! :)

Actual result:
Parse error: parse error, unexpected '[' 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-10 00:29 UTC] dan at wep dot net
The parse error thrown complains about the print(someData['foo']) line. Sorry if this wasn't clear. Also expected output would probably be more like "bar ..var_dump output.. bar.
 [2003-10-10 12:24 UTC]
This is an easier verification:
php -r 'class t{const c=array(1=>"Hello\n");} echo t::c[1];'
 [2003-12-01 20:37 UTC] a at b dot c dot de
This isn't restricted to associative arrays in which keys are explicitly specified:

class f { const t = array(7,6,5); }
echo f::t[1];
 [2004-01-09 08:58 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

Arrays in class constants aren't allowed anymore.
 [2004-03-09 07:26 UTC] seva-php at zend dot com
Const arrays is very useful thing! For example when storing error codes etc.

Why isn't it possible just to fix the behavior?
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 18:01:25 2019 UTC