Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most "elegant" way to define a global constant array in PHP

I was wondering what do you think would be the best and cleanest way to define a constant array variable similar to the way define function works. I've seen a lot of people asking this question in Google and so far the easiest solution I have come up with is to use the PHP serialize function inside the define statement, like this

define ("MY_ARRAY", serialize (array ("key1" => $value1,"key2" => $value2, ..)));

then to use the defined constant you can do something like this:

$MY_ARRAY = unserialize (MY_ARRAY)
print_r ($MY_ARRAY);

Not sure if the serialize function will slow you down if you have a lot of defines in your code. What do you think?

like image 206
techexpert Avatar asked Jan 09 '11 02:01

techexpert


People also ask

How do you declare an array constant in PHP?

Array constants can now be defined using the define() function. In PHP 5.6, they could only be defined using const keyword.

Which of the following is the correct way to declare the constant in PHP?

Create a PHP Constant To create a constant, use the define() function.

How do you declare a global constant?

A constant which is needed in more than one functions can be declared a global constant by declaring it a constant using the reserve word const, initializing it and placing it outside of the body of all the functions, including the main function.

How can use global array variable in PHP?

There are two ways to reference a global variable in PHP: Use the global keyword at the start of every function that uses the variable. Use the $GLOBALS array.


2 Answers

$GLOBALS['MY_ARRAY'] = array();
like image 56
dqhendricks Avatar answered Oct 14 '22 15:10

dqhendricks


The serialization and especially unserialization is pretty awkward. (On the other hand it's not quite clear why a scripting language can't have arrays as constants...)

But it really depends on the usage pattern. Normally you want global defines for storing configuration settings. And global variables and constant are an appropriate use for that (despite the "globals are evil!!1!" meme). But it's advisable to throw everything into some sort of registry object or array at least:

class config {
     var $MY_ARRAY = array("key1"=>...);
     var $data_dir = "/tmp/";
} 

This gives the simplest access syntax with config::$MY_ARRAY. That's not quite an constant, but you can easily fake it. Just use an ArrayObject or ArrayAccess and implement it in a way to make the attributes read-only. (Make offsetSet throw an error.)

If you want a global array constant workaround, then another alternative (I've stolen this idea from the define manual page) is to use a function in lieu of a constant:

function MY_ARRAY() {
     return array("key1" => $value1,);
}

The access is again not quite constantish, but MY_ARRAY() is short enough. Though the nice array access with MY_ARRAY()["key1"] is not possible prior PHP 5.3; but again this could be faked with MY_ARRAY("key1") for example.

like image 29
mario Avatar answered Oct 14 '22 14:10

mario