473,508 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2683
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1837
by: Klaus Neuner | last post by:
Hello, Python has one feature that I really hate: There are certain special names like 'file' and 'dict' with a predefined meaning. Yet, it is allowed to redefine these special names as in ...
0
10158
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
9
1657
by: danny van elsen | last post by:
hello all, I have an application in which I build a list<node>, with potentially thousands of nodes. each node has an "index", and all nodes are ordered by this index. this index reflects a...
33
2791
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
25
5299
by: prabhat143 | last post by:
Hi, Given a singly linked, null terminated list, how can it be converted to tree? Each node in the list has three attributes: it's ID, it's parent ID and of course, the next node it's pointing...
1
1386
by: Le Poisson | last post by:
My apologies if my question sounds childish. Please explain the following code to me. I found it at the beginning of a script. if (isset($_REQUEST) OR isset($_FILES)) { echo 'Request tainting...
5
12800
by: ampeloso | last post by:
Hello, I would like to allocte memory, but I want it to start at a predefined address. I have a program that writes data to ROM in an embedded device and I would like to state where it goes. Can...
3
5830
by: bkamrani | last post by:
Hello, Sorry guys for this newbie questions. But I wonder if there is a standard or build-in method to know the methods of a class? I'm not originally a progrommer and I have worked with...
3
6347
by: Mister Joe | last post by:
I've tried searching w/ google and on php.net and still can't find a complete list off all exceptions that are predefined. I need a null object exception and don't want to write one if one already...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.