get_defined_vars doesn't return superglobals when...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Oren wrote:it is defined and available within the function too.
only one of those is correct ;)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

feyd wrote:You're having a real hard time understanding the differences between defined and available, aren't you? PHP is doing magic behind the scenes to make the request for $_GET['var'] to work. That's why they're called superglobals. They're defined once, available everywhere.
Errrrrrrrrr :evil: :evil: :evil: :evil:
Did I say something else? When did you hear me saying they are defined more than once?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Jenk wrote:
Oren wrote:it is defined and available within the function too.
only one of those is correct ;)
You keep getting me wrong... I didn't say it is being defined (a second time) withing the function, what I was trying to say was: it's already defined inside the function.

Edit: I really don't know... Maybe it has something to do with my english :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The function retrieves variables that have been defined in the context it is called it. The superglobals were defined in the global context therefore do not list in a function.

I'm amazed this thread has gone this far..
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

the function is called get_defined_vars. It does this:

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

Superglobals are defined outside the scope of your function. The only reason that you can use them inside of a function is because php makes them available inside the function behind the scenes. It does not define them there. Therefor, it does not show up when get_defined_vars() is called.

Image
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

feyd wrote:The function retrieves variables that have been defined in the context it is called it. The superglobals were defined in the global context therefore do not list in a function.
Finally, now I get you and you get me :D
I don't know whether it's my english or the manual, but this (from the manual):

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

doesn't sound the same (to me at least) as this:
feyd wrote:The function retrieves variables that have been defined in the context it is called it. The superglobals were defined in the global context therefore do not list in a function.
Edit: After a second thought, I believe it's the manual and not my english. What do you, the native english speakers, think?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The manual is just fine for me... but I can read technical manuals with great ease.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

feyd wrote:You're having a real hard time understanding the differences between defined and available, aren't you? PHP is doing magic behind the scenes to make the request for $_GET['var'] to work. That's why they're called superglobals. They're defined once, available everywhere.
Well that's the answer ... but I can't say it's a very good one. The word defined is used in the manual for constants, functions and classes. Then "defined" is used for variables when discssing scope and specifically for "Pre-defined" variables which includes the super-globals.

It looks like the combination of a poor use of terms and a practical decision not to clutter up the array returned by get_defined_vars() with superglobals and probably internals naming cruft. I think a better term might have been "declared" when discussing variables.

Code: Select all

$glo = 1;
function check() {
     global $glo;
     global $_GET;

     $loc = 2;
     print_r(get_defined_vars());
}
check();
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

arborint wrote:I think a better term might have been "declared" when discussing variables.
There's a distinction in programming languages between "declared" and "defined" however. I would probably prefer "instantiated." Although I can't expect most people to spell that one right. :D
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

arborint wrote:

Code: Select all

$glo = 1;
function check() {
     global $glo;
     global $_GET;

     $loc = 2;
     print_r(get_defined_vars());
}
check();
That's interesting... It prints:

Code: Select all

Array
(
    [glo] => 1
    [loc] => 2
)
$glo was defined within the global scope and not within the function's scope.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The global keyword makes it defined. It's still the same situation as before with a superglobal though. PHP's just ignoring the request, because it's silly.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

feyd wrote:The global keyword makes it defined. It's still the same situation as before with a superglobal though. PHP's just ignoring the request, because it's silly.
Yes, but it has never been defined within the function's scope, thefore it shouldn't be part of the result - which proves that either the definition of this function in the manual is wrong or the function itself has some problems and it doesn't work as it should.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you truely feel that way then I suggest you go search the bug database. If there isn't a bug on it, make one.

http://bugs.php.net
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Well, before I do that, what do you think? Do you agree with me?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The function works perfectly fine for my needs. .. not that I ever use it.
Post Reply