Page 1 of 1

Defines or arrays?

Posted: Tue Dec 05, 2006 1:38 pm
by Technocrat
I am trying to figure out the best course of action to handle holding lagnuage values.

For example:
language_english.php would have
define('_FOO','foo');

or

$lang['foo'] = 'foo';
$GLOBALS['lang_english'] = $lang['foo'];

Anyone have any thoughts, or even a better suggestion? Some on told me that defines are slower than arrays, but arrays use more memory.

Posted: Tue Dec 05, 2006 2:28 pm
by ok
If you don't use OOP, I suggest <$lang['foo'] = 'foo'; > since globals can make things dirty and with many constants is a big mess.

But...

If you use OOP and a loader, you can just share the $lang array with the other classes.

Posted: Tue Dec 05, 2006 6:49 pm
by Technocrat
We are using OOP in many cases.

I was thinking that maybe having the language inside of all the classes would only server to make them bigger.

Posted: Tue Dec 05, 2006 7:09 pm
by Ollie Saunders
In programming very little is black and white. If you try to make black and white decisions you will end up making the wrong ones far too often. In this case you need to judge whether to use constants (defines) over arrays on a case by case basis. Also these two things are fundamentally different; make sure you understand what they both imply.

Posted: Tue Dec 05, 2006 7:12 pm
by John Cartwright
I would defiantly keep things stored in an array simply because it is the cleanest approach IMO. Premature optimization is not a good thing, and you really shouldn't be concerned about memory with a simple language storage array.

Posted: Tue Dec 05, 2006 7:15 pm
by Technocrat
I have used both before. Personally I kind of like using the array approach because it's cleaner to me. I guess I was just looking for more feed back or validation to approach. Or maybe even a better way to do it.

Posted: Tue Dec 05, 2006 9:01 pm
by neophyte
I sometimes store my data in a data object like so:

Code: Select all

$data->foo = bar;
Saves a few key strokes ['']. ;)

And you can still loop through it with foreach.

And in a pinch if you need to access it as an array you just...

Code: Select all

$data = (array) $data;
And it magically becomes an array. And when your done switch it back to an object.

Code: Select all

$data = (object) $data;