Variable References and Objects. fun fun fun
| From: | Simon Edwards | Date: | Sat, 12 Aug 2000 07:31:22 +0000 |
| Subject: | Variable References and Objects. fun fun fun | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-11442@lists.php.net to get a copy of this message | ||
Hi,
How should I create an object (into var i) and move it to another var
(j) without copying it, and then not having the original var (i)
associated with the object?
Here is a method that I found works.
1. class x {
2. var $blah;
3. }
4. $i = new x();
5. $i->blah = "foo";
6. $j = &$i;
7. print "i is ".$i->blah."\n"; // 'foo'
8. print "j is ".$j->blah."\n"; // 'foo'
9. $i->blah = "bar"; // Prove link.
10. print "set to bar\n";
11. print "i is ".$i->blah."\n"; // 'bar'
12. print "j is ".$j->blah."\n"; // 'bar'
13. unset($i); // Unset $i to break the link, else we kill the object.
14. $i = null;
15. print "null i\n";
16. print "i is ".$i->blah."\n"; // nothing
17. print "j is ".$j->blah."\n"; // 'bar'
Line 13 is the hack. Is there are better way that doesn't involve using
unset()?
If I omit line 13 the object gets killed/nulled. Perhaps php should
support something like:
$i = &null; // null or any other expression.
where $i is assigned null but if $i is a reference it is not
dereferenced first. Basically, storage should be made for the result of
the expression and $i made a reference to that storage. (Kind of like
temporarily 'naming' the expression with a var name and creating a ref
to the temp var name).
food for thought...
--
Simon Edwards
Animated Design, Melbourne
http://www.animated.net.au/ Ph: (03) 98850990