Re: Global Annoyance
| From: | Matt McClanahan | Date: | Mon, 18 Dec 2000 20:59:12 +0000 |
| Subject: | Re: Global Annoyance | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-30918@lists.php.net to get a copy of this message | ||
On Mon, 18 Dec 2000 Bill.Hoffman@walgreens.com wrote:
> 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.
Well, one option would be to extract($GLOBALS) in your functions, which
will define variables for each element of the $GLOBALS array.
http://www.php.net/manual/function.extract.php
However, if your issue is that you want those five variables in each
function, and don't care about the other globals, you could also write a
function to grab those five variables, and extract() them.
function get_my_vars()
{
$vars = Array('var1','var2','var3');
foreach ($vars as $var)
{
global $$var;
$retvars[$var] = $$var;
}
return $retvars;
}
And in your functions, just run extract(get_my_vars()) in place of your
global calls.
> Please respond to me via direct E-mail. The list stopped mailing to me months ago.
Try unsub/resubbing.
Matt