Forum Moderators: coopster
But I'm not too sure. i would suggest trying different things. Other than this, I primarily set constants with define() [us2.php.net].
My hack at constant arrays :function carray($const = null, $index = null)
{
static $carrays;if ($const === null)
{
$i = count($carrays);
$carrays[$i] = $index;
return $i;
} else
{
return $index === null? $carrays[$const] : $carrays[$const][$index];
}
}
function aconst()
{
$args = func_get_args();
if (count($args) == 1 && is_array($args[0]))
{
return carray(null, $args[0]);
} else
{
return carray(null, $args);
}
}Use like this:
define('NRWORDS', aconst('zero', 'one', 'two', 'three'));
define('STUNT_DESC', aconst(array(
STUNT_TWOWHEELS => 'Two wheels',
STUNT_WHEELIE => 'Wheelie',
STUNT_STOPPIE => 'Stoppie'
)));print(carray(NRWORDS, 2));
print(carray(STUNT_DESC, STUNT_WHEELIE));
$arr = carray(STUNT_DESC);print_r($arr[STUNT_STOPPIE]);
bvr.
define ("MAXSIZE", 100);
echo MAXSIZE;
I'm not sure if you can do that with an array. (If you could, it would probably add confusion). I would just use a variable array:
$arr = array("one", "two", "three");
And it should basically behave the same way. If you need it to be global, then do:
global $arr;
inside every function where you're using it.
Only scalar data (boolean, integer, float and string) can be contained in constants.
Constants [php.net]