Defines or arrays?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Defines or arrays?

Post 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.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post 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.
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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;
Post Reply