Re: trying to understand zvals
| From: | Stanislav Malyshev | Date: | Thu, 17 Oct 2002 14:34:54 +0000 |
| Subject: | Re: trying to understand zvals | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-89655@lists.php.net to get a copy of this message | ||
TDJ>> I'm trying to do some extension programming, and I'm pretty confused
TDJ>> by the whole zval thing. In particular, references are a little
TDJ>> mysterious. If I have
TDJ>>
TDJ>> $foo = "zonk";
TDJ>> $bar =& $foo;
TDJ>>
TDJ>> in PHP, what actually happens? Specifically, if I wanted a function
What actually happens is that both entries in symbol table with names
"foo" and "bar" point to the same zval (having refcount of 2 now)
containing string "zonk".
TDJ>> that did such a reference assignment:
TDJ>>
TDJ>> $foo = "zonk";
TDJ>> $bar = "baz";
TDJ>>
TDJ>> ref_assign($bar, $foo); // $bar =& $foo;
TDJ>>
TDJ>> what has to happen in ref_assign?
That's problematic, due to the way PHP variables work. The best you can do
is something like this:
function ref_assign($a, $b) {
$GLOBALS[$a] =& $GLOBALS[$b];
}
ref_assign("bar", "foo");
That's assuming they are globals. If they are not, I cannot now think of a
good way to make variable-binding function.
--
Stanislav Malyshev, Zend Products Engineer
stas@zend.com http://www.zend.com/ +972-3-6139665 ext.109