Re: Global Annoyance
| From: | Rasmus Lerdorf | Date: | Mon, 18 Dec 2000 23:31:14 +0000 |
| Subject: | Re: Global Annoyance | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-30948@lists.php.net to get a copy of this message | ||
Yes, this is a regular complaint, but let me give you the history on this
one.
When I was writing the first version of PHP I had a real day job. In this
real day job I was one of a couple of developers on a very large project.
And we had a bug. Nobody could find this bug and the deadline was getting
closer. We single-stepped, we added break points, we debugged for days.
I finally got extremely stubborn and printed out everything. All 350,000
lines of code on a dot-matrix printer and laid out the code in the
hallways where I worked. I fought with the cleaning crews as I crawled
along on my hands and knees with different coloured pens and traced
through everything. After 2 16 hour sessions I found the problem. It was
a function that changed a global by mistake as a side effect.
At that point I swore that I would never have that problem again. I
mandated a rule that forced everyone to list the global variables they
used in comments at the top of every function. However, as I was also
writing PHP at the time, I had the chance to force this right at the
language level. If you forgot to declare your variable global then the
variable would simply be local to the function and the function probably
wouldn't work as expected, but at least it would not cause weird side
effects other distant parts of the code and thus a local/global mistake
would be much much easier to track down.
So that is why this is so. I know it annoys some people, especially
people who use a lot of global variables, but that is actually a benefit.
It encourages people to use less global variables and it forces you to
think about each global variable you use.
PHP is pretty free-form and there aren't too many places where the PHP
developers have enforced their own views on the PHP users, but this is one
place where you just have to trust me. In the long run you are better
off.
-Rasmus
On Mon, 18 Dec 2000 Bill.Hoffman@walgreens.com wrote:
> 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:
>
> function MailIt () {
> global $rec;
> global $subject;
> global $boxstatus;
> global $box;
> global $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 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
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>