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

List predefined variables

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

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