By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,919 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,919 IT Pros & Developers. It's quick & easy.

Declare variable as superglobal?

P: n/a
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 -

Aug 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 23 Aug 2005 12:01:04 -0700, "Super Mango" <li********@gmail.com> wrote:
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?


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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 23 '05 #2

P: n/a
On 2005-08-23, Super Mango <li********@gmail.com> wrote:
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?


You are aware of $_GLOBALS right?

--
Cheers,
- Jacob Atzen
Aug 23 '05 #3

P: n/a
JDS
On Tue, 23 Aug 2005 12:01:04 -0700, Super Mango wrote:
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 -


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 | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Aug 23 '05 #4

P: n/a
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.

Aug 23 '05 #5

P: n/a
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.

Aug 23 '05 #6

P: n/a
Super Mango wrote:
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.


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

echo br;

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 24 '05 #7

P: n/a
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.

Aug 24 '05 #8

P: n/a
Super Mango schrieb:
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)?


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
Aug 24 '05 #9

P: n/a
Super Mango wrote:
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?


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

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

Aug 25 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.