Page 1 of 1

*Long-Lived* Variable Naming: Is $VAR or $ARRAY too generic?

Posted: Tue Apr 19, 2005 9:00 pm
by Ambush Commander
I have a script, and in this script, I define a few variables: these are $VAR, $ARRAY, $PRE and $IS. They exist during the entire script, but are never "globalized" into functions or other classes: that is, they are simply data collection, so that variables can be quickly passed to the template system. The reason why I am doing this is because:

Code: Select all

$smarty->assign("is",array('destroyed'=>true,'clean'=>false)
Is much longer and harder than just doing:

Code: Select all

$IS['destroyed'] = true;
//Lots of stuff
$IS['clean'] = false;
$smarty->assign("is",$IS);
Now, a few questions:

1. Is my mentality correct, or am I barking up the wrong tree? Should I use a class, as in:

Code: Select all

$VAR_REP->IS['destroyed'] = false;
(see, I prefer the first way because it's much less code, and easier to reference back to later on.)

2. What is the probability that my generic variables $IS and $VAR and $ARRAY are going to get clobbered by the inclusion of foreign libraries?

Posted: Tue Apr 19, 2005 9:07 pm
by McGruff
For OOP programmers, globals are evil - like finding a caterpillar in you sandwich. If I'm looking at a new php app I'll instantly move on and look elsewhere if I see globals (or a lack of unit tests).

Avoiding globals might seem hard but it can be done. Trying to eliminate them will eventually lead you towards a well encapsulated OOP design.

Posted: Tue Apr 19, 2005 9:31 pm
by Ambush Commander
Well, actually, they're technically not globals, because I'm not:

Code: Select all

function foo()
{
    global $VAR;
}
doing that. They're just really long lived in their natural scope.

Posted: Wed Apr 20, 2005 12:24 am
by McGruff
If it's in the $GLOBALS array, it's a global var. As you say they could indeed cause problems if you use other code which sets globals with the same names.

I'd definitely want to encourage OOP but of course you don't have to code like that Many php apps don't. OOP takes a while to learn but, combined with unit testing, it's a much more powerful way to work.

Posted: Wed Apr 20, 2005 2:47 am
by patrikG
If you want to jump into the deeper end of OOP - the registry pattern would be of interest to you: http://www.phppatterns.com/index.php/ar ... ew/75/1/1/

Posted: Wed Apr 20, 2005 5:19 pm
by Ambush Commander
Referring to patrikG's link about Registry patterns and Singletons: I had been reading about those, but the text relied heavily on the new OOP features PHP5 provided that I dismissed them as impossible to implement in PHP4... this might be the solution. The main point is that while I was learning to use Smarty, I found myself having to constantly do this:

Code: Select all

$smarty->assign('page_name','The Observer');
$smarty->assign('subtitle','Always Always!');
//...
Then I did this:

Code: Select all

$headers['page_title'] = 'The Observer';
$headers['subtitle'] = 'Always Always';
$smarty->assign('headers',$headers);
But the naming conventions were terribly ad hoc, so finally, I decided:

Code: Select all

//Only use $VAR, $ARRAY, $PRE, $IS
$VAR['page_title'] = 'The Observer';
$VAR['subtitle'] = 'Always Always';
$IS['display_subtitle'] = false;
$smarty->assign('var',$VAR);
$smarty->assign('is',$IS);
So, according to the link, what you're proposing is I do this:

Code: Select all

$registry = new Registry;
$registry->is['display_subtitle'] = false;
//or...
//(note how the array key is added, for a type of 'hierarchy'
$registry->assign('is','display_subtitle',false);
if ($registry->is['display_subtitle']) {
  //parse subtitle
}
$smarty->assign('base',$registry); //pass the registry in
Or basically, make my own registry (don't use smarty) for storing variables, and then pass it to smarty?

-_-" My code is still way too procedural.