|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2003-10-10 00:24 UTC] dan at wep dot net
 Description:
------------
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:
----------------
bar
bar
bar
.. Jackpot! :)
Actual result:
--------------
Parse error: parse error, unexpected '[' 
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 14:00:01 2025 UTC | 
This is an easier verification: php -r 'class t{const c=array(1=>"Hello\n");} echo t::c[1];'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];