I don’t have a specific situation I’m dealing with, so consider this more of a general question. If language is important, answers can tend more toward PHP.
If I have a large array (or other data type) of data which I require in multiple functions, that does not change, how should I use that data? For example, maybe I have an array that looks like
$states = array(
"AL" => "Alabama",
...
"WY" => "Wyoming"
);
Obviously, this array won’t need to change anytime soon. If I have five functions that have to use the above array, what is the best solution? My three ideas are:
- Declare it as a global constant
This one seems the most straightforward, however some people I’ve talked to seem to recommend shying away from using global variables. Any insight here would be appreciated.
- Pass it into the function
This one seems like a bad idea to me because it never changes, and function parameters should be for variables, right? Not to mention having to pass it into a function that doesn’t use it, just so it can be used by another function. That seems like a poor practice.
- Have it be returned by a function
This is one that I haven’t seen used a lot, but I’ve used myself a couple of times for passing mysqli_connect() information to multiple functions. It worked quite well. Is this regarded as poor practice? Should I just suck it up and use a global constant?
I realize that using a global constant sounds obvious, but I’ve heard (and read) more than one mantra like, “if you’re declaring globals, you’re doing it wrong” and stuff like that. Can anyone explain why this might be?
Thanks for any insight that you can give me, guys.
My general advice would be to build a “service provider” class, which is analogous to your “return it from a function” option. Here are some specific thoughts:
First, constants can only be scalar values, so you cannot technically have an “constant” array. Second, what you say is almost always true: globals are a hint that there is a better way (with some exception).
If they are actually constant (in the “real world” sense, not the computer-science sense), then you are right, passing to a function should be considered code-clutter (unless the functions should be abstracted from the knowledge of the “global” value, but that’s a domain-specific architecture choice.
Ding Ding! The reason is essentially this: Anyone can access these to “read”, but only the “function” can “write”. I put “function” in quotes because it could also be a class or singleton object instance. Basically, you provide a semantically appropriate provider of this static information.
One of the benefits of this approach is that you may only need the data quite seldom in a long-running program. The service provider may in that case be written to fetch the values from some out-of-memory persistence and free the memory when it is not needed anymore. Nobody else should need to be responsible for that memory management. Along the same lines, imagine that the project grows and these datum need to be loaded from a database instead of a static array… if you have centralized the access, this is remarkably easy to facilitate and you don’t need to have a huge heap of memory lying around for the entire length of your program’s execution holding values that are almost never accessed.