70

Possible Duplicate:
Is it possible to declare an array as constant

Is it possible to use an array as a class constant in PHP?

I.e

const MYARRAY = array('123', '234');

If not why?

Community
  • 1
  • 1
Martin
  • 815
  • 1
  • 7
  • 6
  • 1
    [please use the search function before asking questions. we expect you to do research.](http://stackoverflow.com/questions/ask-advice) – Gordon Jun 25 '12 at 07:15
  • 15
    The duplicate discussed constants created using define(). Rules for class constants are not the same. Voting to reopen. – Ja͢ck Apr 10 '13 at 06:56
  • You can somewhat convert an array to a string and store it as a constant. When you need it, you just reconvert it. Look into the (un)serialize example: http://stackoverflow.com/questions/1290318/php-constants-containing-arrays – Armfoot Jun 27 '13 at 07:58
  • 1
    @Jack, I agree because I find a way to do what I need (xdazz answer) thanks to this question - we aren't only interested in answer like "No, it isn't posibble", but in that which tells us how to do what we want. – Line Apr 25 '14 at 12:41

2 Answers2

76

No, you can't.

But you could declare it as a static property.

public static $MYARRAY = array('123', '234');

---------------Update-----------------------------

Array const is available from PHP 5.6.

php.net/manual/en/migration56.new-features.php

xdazz
  • 158,678
  • 38
  • 247
  • 274
68

UPDATE:

This is now available in PHP 5.6 https://php.net/manual/en/migration56.new-features.php


No you can't assign an Array to PHP constant.

In http://www.php.net/manual/en/language.constants.syntax.php

Constants may only evaluate to scalar values

This is the reason.

Scalar values for examples are int, float, string

Alvin Wong
  • 12,210
  • 5
  • 51
  • 77