Re: "waldschrotts guide to nifty references" - manual pagedraft, version 0.9b
| From: | Stanislav Malyshev | Date: | Sun, 20 Aug 2000 05:40:09 +0000 |
| Subject: | Re: "waldschrotts guide to nifty references" - manual pagedraft, version 0.9b | ||
| References: | 1 | Groups: | php.dev php.qa |
| Request: | Send a blank email to php-qa+get-852@lists.php.net to get a copy of this message | ||
RC>> You can create multiple aliases for the same variable in PHP, by
RC>> using the "&"-sign to establish other references to it..
That's wrong. "=&" is an operator, not &.
RC>> "&" tells PHP to create another reference to the already
RC>> existing variable . It╢s a second reference, because you╢ve
That's wrong too. "=&" does not "create another reference to a
variable". Please read the manual chapter I wrote. This operation binds
two names to the same variable.
RC>> distinguish which one is a reference and which one is a
RC>> variable.
That's because there's no such thing as "which one". Every referee to a
variable is equal. Every menation of $var is reference.
RC>> References are different from pointers in C, and Java
RC>> references, but are very similar to references in C++.
AFAIK, in C++ you cannot bind references like in PHP. Parameter passing is
closer, but C++ has more rich semantics, with copy constructors, etc.
RC>> You╢re also able to return references, so variables can survive
RC>> function calls without using global variables.
That's not that they would survive - that's that you can pass internal
zval to outside. All external variabels survive function calls even
without that, unless function calls explicitly unset them.
RC>> Prepending the function name with the "&"-sign indicates that
RC>> you╢re going to return a reference.
RC>> It╢s not sufficient to assign that reference afterwards with the
RC>> assignement operator, like you╢d do it with the new() statement,
RC>> you have to tell PHP explicitly that it should assign this
RC>> reference again.
Word "again" here very unfortunate. It's not "again". What happens on
return, you tell function "keep zval I pointer in return as is and list it
as return parameter (without copying)". Now, when function returned, PHP
just got a zval, and now comes the assignment (it shouldn't really be
assignment in all cases, function call may be used in many contexts
besides assignment rvalue), PHP should choose semantics of the assignment
- reference-binding or copy. The =& operator makes it choose
reference-binding sematics, which indeed makes sense when you still want
that zval mentioned above without copying it.
RC>> Re-Referencing of multiple variable names, bound to a certain
RC>> variable, to be referenced to another variable, does not work in
RC>> PHP, if you╢ve understood the concept of references in PHP
It works, but not as you want. You can't "assign reference via reference",
because =& is not assignment, it's symbol table binding.
RC>> Thanks to these indirectional pointers (references) it╢s
References *are not* indirectional pointers. They are symbol table
bindings. This is not the same. Difference is roughly like difference
between hardlinks and symlinks in Unix.
RC>> possible to create true recursive data structures, which can be
RC>> very handy in object-orientated communication mechanisms.
Well, unfortunately you can't do containers very well with
references. That's where PHP is not quite there.
RC>> The use of circular references causes memory leaks which may be
RC>> cleaned up after each request in same cases (the more complex,
RC>> the greter likelyhood of a leak), This is intended and should
RC>> not hurt unless you╢re using extremely large objects, or having
RC>> many concurrent scripts running.
I won't mention this in the manual. At least, not the leaks. This doesn't
look good, like "they have a bug and are too lazy to correct it". And if
we start explaining why is that leak and when it happens (which I
personally don't exactly know) and what to do with them - we'll have
entire article of reference counting systems there.
--
Stanislav Malyshev stas@zend.com
+972-3-6139665 ext.106