Re: Global Annoyance
| From: | jeremy brand | Date: | Mon, 18 Dec 2000 22:42:26 +0000 |
| Subject: | Re: Global Annoyance | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-30951@lists.php.net to get a copy of this message | ||
Hi Bill,
For the record, I like having to specify global in each function.
What Rasmus says is true and I believe it does force those less
experienced to think about what they are doing, when otherwise they
might not think about it.
Yes, I can see how your functions seem messy. When I need to do
something like this, I store things in an array. Say for web_form
validation for example:
in my HTML:
<input name="form[name]" ...
<input name="form[email]" ...
<input name="form[fav_deer]" ...
or in PHP setting them specifically:
$form['name'] = 'foo bar clause';
$form['email'] = 'foo@northpole.com';
$form['fav_deer'] = 'blitzen';
Then when you need to use all of these pieces of data as global, all
you do is this:
function do_foo()
{
global $form;
// whatever else
//return something;
}
Hope this helps.
-jeremy brand
Jeremy Brand :: Sr. Software Engineer :: 408-245-9058 :: jeremy@nirvani.net
http://www.JeremyBrand.com/Jeremy/Brand/Jeremy_Brand.html
for more
Get your own Free, Private email at http://www.smackdown.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
At least for the people who send me mail about a new language that
they're designing, the general advice is: do it to learn about
how to write a compiler. --Dennis Ritchie
On Mon, 18 Dec 2000, Rasmus Lerdorf wrote:
> Date: Mon, 18 Dec 2000 15:31:14 -0800 (PST)
> From: Rasmus Lerdorf <rasmus@php.net>
> To: Bill.Hoffman@walgreens.com
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Global Annoyance
>
> 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
> >
>
>
> --
> 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
>
>