Connecting Tech Pros Worldwide Forums | Help | Site Map

Declare variable as superglobal?

Super Mango
Guest
 
Posts: n/a
#1: Aug 23 '05
Hi -

Is it possible to change the status of a variable to superglobal so
it'll be valid inside functions without declaring it with "global"
inside each function?

Thanks -




Andy Hassall
Guest
 
Posts: n/a
#2: Aug 23 '05

re: Declare variable as superglobal?


On 23 Aug 2005 12:01:04 -0700, "Super Mango" <liebermann@gmail.com> wrote:
[color=blue]
>Is it possible to change the status of a variable to superglobal so
>it'll be valid inside functions without declaring it with "global"
>inside each function?[/color]

Not as far as I'm aware, I don't think this is exposed through a PHP API.

I wonder if it's possible to write a loadable extension that could call the
appropriate Zend API to superglobalify a variable, though.

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jacob Atzen
Guest
 
Posts: n/a
#3: Aug 23 '05

re: Declare variable as superglobal?


On 2005-08-23, Super Mango <liebermann@gmail.com> wrote:[color=blue]
> Is it possible to change the status of a variable to superglobal so
> it'll be valid inside functions without declaring it with "global"
> inside each function?[/color]

You are aware of $_GLOBALS right?

--
Cheers,
- Jacob Atzen
JDS
Guest
 
Posts: n/a
#4: Aug 23 '05

re: Declare variable as superglobal?


On Tue, 23 Aug 2005 12:01:04 -0700, Super Mango wrote:
[color=blue]
> Hi -
>
> Is it possible to change the status of a variable to superglobal so
> it'll be valid inside functions without declaring it with "global"
> inside each function?
>
> Thanks -[/color]

All you have to do is put it inside one of the superglobal arrays. Use
those arrays directly inside the function.

e.g. instead of

function donut(){
global $holes;
return $holes - 1;
}

do

function donut(){
return $GLOBALS['holes'] - 1;
/* or $_SESSION['holes'] or $_POST['holes'] or other superglobal */
}

However, the real answer to your question is DO NOT USE GLOBALS INSIDE
FUNCTIONS!!!! You are just asking for trouble down the line, take my word
for it.

--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Chung Leong
Guest
 
Posts: n/a
#5: Aug 23 '05

re: Declare variable as superglobal?


I don't think that can be implemented. The list of autoglobals isn't
stored in a pre-request structure. If you append a new variable to it,
it's going to remain there after a script finishes executing. You can
certainly write an extension that uses its own autoglobals however.

Super Mango
Guest
 
Posts: n/a
#6: Aug 23 '05

re: Declare variable as superglobal?


Thanks,

My idea is to set variables like
$b="<BR>"
so it's kind of shortcuts.
If I use the super globals, I'd rather write "<BR>" directly.

Sorry for not explaining that in the first place.

Jerry Stuckle
Guest
 
Posts: n/a
#7: Aug 24 '05

re: Declare variable as superglobal?


Super Mango wrote:[color=blue]
> Thanks,
>
> My idea is to set variables like
> $b="<BR>"
> so it's kind of shortcuts.
> If I use the super globals, I'd rather write "<BR>" directly.
>
> Sorry for not explaining that in the first place.
>[/color]

define('br', '<br>');

echo br;

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Super Mango
Guest
 
Posts: n/a
#8: Aug 24 '05

re: Declare variable as superglobal?


Great! The "define" did it.

Any Idea for the cases that I want to use a variable after all (for
counter or for mysql connection)?

Thanks again.

Oliver Grätz
Guest
 
Posts: n/a
#9: Aug 24 '05

re: Declare variable as superglobal?


Super Mango schrieb:[color=blue]
> Great! The "define" did it.
>
> Any Idea for the cases that I want to use a variable after all (for
> counter or for mysql connection)?[/color]

Good you elaborate on that?

For example, I hide the connecting stuff from everyday code. Looks
something like this:

class MySQL
{
private static $connection=null;
protected static function connect()
{
if (self::$connection != null) return;
self::$connection=mysql_connect('bla','blah','bla' );
// read from config...
}
public function query($sql)
{
self::connect();
return mysql_query(self::$connection,$sql);
}
}

In my everyday code I just have to

$result=MySQL::query('SELECT * FROM test');

No explicit connecting required. Of course you can go for functions but
I'm all for the OOP way (even if it's just static stuff like here).

AllOLLi
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#10: Aug 25 '05

re: Declare variable as superglobal?


Super Mango wrote:[color=blue]
> Hi -
>
> Is it possible to change the status of a variable to superglobal so
> it'll be valid inside functions without declaring it with "global"
> inside each function?[/color]

runkit <http://in.php.net/runkit>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Closed Thread