Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 23rd, 2005, 08:05 PM
Super Mango
Guest
 
Posts: n/a
Default Declare variable as superglobal?

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 -

  #2  
Old August 23rd, 2005, 08:15 PM
Andy Hassall
Guest
 
Posts: n/a
Default 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
  #3  
Old August 23rd, 2005, 08:25 PM
Jacob Atzen
Guest
 
Posts: n/a
Default 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
  #4  
Old August 23rd, 2005, 09:05 PM
JDS
Guest
 
Posts: n/a
Default 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/

  #5  
Old August 23rd, 2005, 10:05 PM
Chung Leong
Guest
 
Posts: n/a
Default 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.

  #6  
Old August 23rd, 2005, 11:15 PM
Super Mango
Guest
 
Posts: n/a
Default 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.

  #7  
Old August 24th, 2005, 01:15 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default 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
==================
  #8  
Old August 24th, 2005, 03:05 PM
Super Mango
Guest
 
Posts: n/a
Default 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.

  #9  
Old August 24th, 2005, 04:05 PM
Oliver Grätz
Guest
 
Posts: n/a
Default 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
  #10  
Old August 25th, 2005, 12:05 PM
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles