Re: Global Annoyance

From: Date: Mon, 18 Dec 2000 21:12:00 +0000
Subject: Re: Global Annoyance
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-30928@lists.php.net to get a copy of this message
We get around this somehow by using objects. $obj->rec; $obj->subject; $obj->box; etc. Then passing just the $obj to the function. Or, if you wanted to avoid that step, simply globalizing $obj. Yes, it's still not ideal, but less GLOBAL statements to remember. HTH. 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, ========================== Michael Kimsal http://www.tapinternet.com

« previous php.general (#30928) next »