472,119 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

List predefined variables

Is there any way to tell PHP predefined variables ($GLOBALS, $argv,
$argc, $_GET, $_POST…) from *global* user-defined variables? Neither
$GLOBALS nor get_defined_vars() put user data apart.

I’m writing a class to generate PHP definition files for syntax
highlighting with two features:

1. It takes data from current install (e.g., you get function names from
loaded extensions).
2. You can choose whether to include user data or not.

It’s been very easy with functions and constants but I can’t figure out
how to deal with variables, rather than hard-coding the names of
predefined vars... I’ve kind of automated this hard-coding but it still
smells of lame workaround.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Oct 2 '08 #1
2 2595
On 2 Oct, 12:24, "Álvaro G. Vicario"
<alvaroNOSPAMTHA...@demogracia.comwrote:
Is there any way to tell PHP predefined variables ($GLOBALS, $argv,
$argc, $_GET, $_POST…) from *global* user-defined variables? Neither
$GLOBALS nor get_defined_vars() put user data apart.

I’m writing a class to generate PHP definition files for syntax
highlighting with two features:

1. It takes data from current install (e.g., you get function names from
loaded extensions).
2. You can choose whether to include user data or not.

It’s been very easy with functions and constants but I can’t figure out
how to deal with variables, rather than hard-coding the names of
predefined vars... I’ve kind of automated this hard-coding but it still
smells of lame workaround.
Why not just iterate over $_GLOBALS, and for each entry, check if it
is in scope:

function get_non_superglobal_globals()
{
$result=array();
foreach ($_GLOBALS as $anon_name =$anon_value) {
if (($anon_name!='anon_name') && ($anon_name!='anon_value')) {
if ($_GLOBALS[$anon_name]===$$anon_name) {
// its a superglobal
} else {
// its a user global
$result[]=$anon_name;
}
}
}
}

- this is bit of a hack too - I think there are circumstances where it
won't work. I'd go with a predefined list of superglobals.

C.
Oct 3 '08 #2
C. (http://symcbean.blogspot.com/) escribió:
>Is there any way to tell PHP predefined variables ($GLOBALS, $argv,
$argc, $_GET, $_POST…) from *global* user-defined variables? Neither
$GLOBALS nor get_defined_vars() put user data apart.
function get_non_superglobal_globals()
{
$result=array();
foreach ($_GLOBALS as $anon_name =$anon_value) {
if (($anon_name!='anon_name') && ($anon_name!='anon_value')) {
if ($_GLOBALS[$anon_name]===$$anon_name) {
// its a superglobal
} else {
// its a user global
$result[]=$anon_name;
}
}
}
}
I had tried this approach but it didn't work because superglobals cannot
be used with variable variables. However, I've found a workaround
with... ahem... eval():

<?php
foreach ($GLOBALS as $anon_name =$anon_value) {
if (($anon_name!='anon_name') && ($anon_name!='anon_value')) {
$is_superglobal = eval('return isset($' . $anon_name . ');');
echo $anon_name . ': ' . ($is_superglobal ? 'superglobal' : 'user') .
"\n";
}
}
?>

GLOBALS: superglobal
argv: user
argc: user
_POST: superglobal
_GET: superglobal
_COOKIE: superglobal
_FILES: superglobal
_SERVER: superglobal
my_variable: user
foo: user

Of course, I guess there's nothing I can do with predefined variables
that are not superglobals (e.g. argv & argc).
- this is bit of a hack too - I think there are circumstances where it
won't work. I'd go with a predefined list of superglobals.
Well, I need some predefined lists anyway ("if", "else", "new"...) so I
guess it won't be a great deal. It's easier than writing a Zend
extension ;-)

Thank you for your suggestion.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Oct 3 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Klaus Neuner | last post: by
9 posts views Thread by danny van elsen | last post: by
33 posts views Thread by abs | last post: by
25 posts views Thread by prabhat143 | last post: by
1 post views Thread by Le Poisson | last post: by
5 posts views Thread by ampeloso | last post: by
3 posts views Thread by bkamrani | last post: by
3 posts views Thread by Mister Joe | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.