Request For Comments: A $CURRENT array
| From: | Sterling Hughes | Date: | Wed, 06 Sep 2000 08:26:59 +0000 |
| Subject: | Request For Comments: A $CURRENT array | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-32321@lists.php.net to get a copy of this message | ||
TITLE
$CURRENT, The Current Scope
VERSION
Maintainer: Sterling Hughes <sterling@php.net>
Date: 6 Sep 2000
Mailing List: php-dev
ABSTRACT
A proposal for the $CURRENT variable which would contain all the variables in
the current scope.
DESCRIPTION
Right now PHP has the $GLOBALS variable, a variable that contains all of
entries in the EG(symbol_table), or all variables that are in the global scope.
I'm proposing a $CURRENT variable, a variable that contains all of the entries
in the EG(active_symbol_table), or all variables that are in the current scope.
Take the following example:
<?php
$name = "Sterling Hughes";
$mp3s = array("Sound of Silence",
"Purple Haze",
"Aqualung");
function show_scope ($a, $b, $c)
{
foreach ($CURRENT as $name => $value) {
echo "$name: $value\n";
}
}
show_scope (1, 2, 3);
?>
Which would output:
CURRENT: array
a: 1
b: 2
c: 3
name: c
value: c
The $CURRENT variable would be like any other PHP variable in that you could
also assign values to it (just like with the globals). Those values would then
be put in the current scope:
<?php
// Here $CURRENT == $GLOBALS
$CURRENT[name] = "Sterling Hughes";
$CURRENT[cds] = array("The Chronic",
"Doggystyle",
"The Wall",
"The Doctor");
function just_the_rap ()
{
$CURRENT[name] = "Funkmaster Sterling";
$CURRENT[cds] = array("The Chronic",
"Doggystyle");
foreach ($CURRENT as $na => $val) {
echo "$na: $val\n";
}
}
just_the_rap();
foreach ($CURRENT as $na => $val) {
echo "$name: $value\n";
}
?>
This would output:
CURRENT: array
name: Funkmaster Sterling
cds: Array
na: cds
val: cds
// + OUTPUT of the $GLOBALS array
// without the $CURRENT assignments at the
// top
CURRENT: array
name: Sterling Hughes
cds: Array
na: cds
val: cds
IMPLEMENTATION
Basics
The Implementation of this is very similair to that of the $GLOBALS array. We
create an array (zval) out of the EG(active_symbol_table) and then store this
array in the active symbol table so that it is always accessable. One noteable
difference is that the for the stored variable must be two not one.
Concessions
Since the $CURRENT array will contain a reference to itself, functions like
var_dump() will go through it recursively just like with the $GLOBALS array.
Patch
Here is a patch that makes the $CURRENT array work with the latest CVS of Zend
& PHP4:
Index: Zend/zend_execute.c
===================================================================
RCS file: /repository/Zend/zend_execute.c,v
retrieving revision 1.231
diff -u -r1.231 zend_execute.c
--- Zend/zend_execute.c 2000/08/31 22:24:20 1.231
+++ Zend/zend_execute.c 2000/09/06 06:45:44
@@ -960,6 +960,19 @@
}
}
+ {
+ zval *current;
+ ALLOC_ZVAL(current);
+ current->refcount = 2;
+ current->is_ref = 1;
+ current->type = IS_ARRAY;
+ current->value.ht = EG(active_symbol_table);
+
+ if (zend_hash_add(EG(active_symbol_table), "CURRENT", sizeof("CURRENT"),
¤t, sizeof(zval *), NULL) == FAILURE) {
+ FREE_ZVAL(current);
+ }
+ }
+
#if SUPPORT_INTERACTIVE
while (opline<end) {
#else
REFERENCES
The implementation of the $GLOBALS array, lines 950-960 of
Zend/zend_execute.c. Available online at
http://lxr.php.net/source/Zend/zend_execute.c#950
-Sterling
Attachment: [application/octet-stream] current_diff
Attachment: [application/octet-stream] current_diff