Re: Global Annoyance
| From: | Lars Torben Wilson | Date: | Tue, 19 Dec 2000 05:13:35 +0000 |
| Subject: | Re: Global Annoyance | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-30924@lists.php.net to get a copy of this message | ||
Bill.Hoffman@walgreens.com writes:
> I would first like to state that I have been using PHP for the last
> year and think that it is the greatest thing since sliced bread. There is
> one thing that has been annoying me that I have not found a way around and
> thought I would find out if there were others like me. I do not like
> having to declare a variable as global every time I use it in a function.
> 95% of the time the variables that I use I want to use throughout my whole
> script. I like to use functions but when I do I must declare all of the
> variables that I have already declared again in every function. Here is an
> example in case I am being unclear:
This is a fairly common feeling among people used to other ways of
doing scoping, but it was done for a reason and IMHO it's a lifesaver.
Also, before you read this, realize that this is all very much IMHO
and YMMV and all those other disclamatory acronyms. ;)
Globals have some definite downsides as far as code maintainability
goes. For instance, a function can change a global variable (a
side-effect) and if something goes wrong, you have to go look through
every function that you have, looking for the one which might have
changed it. Full access to globals can also let you tend to write very
tightly-connected code--i.e., where the code inside the function is
highly dependent on what's outside the function. No problem, if you
don't want to pull that function out and use it on another page. The
MailIt() function below is a good example: to use this on another
page, you'd have to make sure the new page had identically matching
global variables which don't already mean something else on that
page. On the other hand, were it declared something like this:
function MailIt($rec, $subj, $status, $box, $body) {
...it wouldn't matter, since you just pass everything needed in the
function call.
This isn't to say that globals are always, unequivocally A Bad
Thing. I often use them to pull in often-accessed PHP-supplied
information which I don't need to change; say $PHP_SELF,
$HTTP_POST_VARS, etc. But only in cases where there isn't a 'cleaner'
way.
But on large projects (say, several thousands of lines or more), where
you have more than one author, you really, really don't want to spend
your time tracking every variable through every function that might
have used or changed it. By explicitly passing all information in the
argument list to a function, just looking at the function call itself
tells you everything you'll need to know about what the function needs
to run.
> function MailIt () {
> global $rec;
> global $subject;
> global $boxstatus;
> global $box;
> global $body;
On a more practical note, you can make the above a bit less painful by
writing it as:
function MailIt() {
global $rec, $subject, $boxstatus, $box, $body;
. . .
}
> # call the mail function to mail admins
> mail
> (
> "$rec",
> "$subject",
> "This E-mail has been sent due to $boxstatus on the currect box $box
>
> $body"
> )
> }
>
> $rec = me@you.com;
> $subject = "test subject";
> $boxstatus = "down";
> $box = "prdbox";
> $body = "This is where the body will go";
> MailIt ();
>
>
> I would like to have all of my variables available globally unless I
> specify otherwise. If I wanted to make a second mail function I
> would have to declare all of these variables all over again. I have
Another bonus to using argument lists. BTW, if you have a zillion
variables you need to send to a function, you can always make things
easier to type by putting them into an array and passing
that. And personally, I like being able to do something like this:
<?php
$tmp = 'Torben';
function foo() {
$tmp = 'Smurfy';
return $tmp;
}
echo "My name is $tmp and I think PHP is " . foo() . "\n";
?>
...and not have it tell me my name is 'Smurfy'. :)
> had to declare the same variable 5 times in 5 different functions.
> Let me know if anyone knows a better way to do this. Please respond
> to me via direct E-mail. The list stopped mailing to me months ago.
> Thanx, BEH
I doubt very much that this particular aspect of PHP is likely to
change anytime soon; it's been beaten to death on the list before a
few times. There are various hacks in the mailing list to get around
it (hint: extract($GLOBALS);) but that's basically your only other way
around it.
Hope this helps,
Torben
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+