2

Since the PHP 5.6 it's possible do define a constant array like this:

const MY_CONSTANT = array('fruit'=>'apple');

However I am not clear on how exactly I can access array elements that use either keys or indexes. PhpStorm complains about a syntax error when I try to do it like this:

self::MY_CONSTANT['fruit'];
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Eugene Krall
  • 503
  • 5
  • 20

1 Answers1

4

The syntax you're using is fine and should work for 5.6+

PhpStorm shows an error because of an outstanding bug; the full support of constant arrays was completed after the first release candidate, so this is not entirely surprising ;-)

Since 8.0.3 you can already define constant arrays, but you need to indirectly reference them, i.e.:

$tmp = self::MY_CONSTANT;
echo $tmp['fruit'];
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309