Re: Variable References and Objects. fun fun fun
| From: | Zeev Suraski | Date: | Sat, 12 Aug 2000 08:14:33 +0000 |
| Subject: | Re: Variable References and Objects. fun fun fun | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-11443@lists.php.net to get a copy of this message | ||
Using unset() is the right way to do it. If you no longer want $i to be
anything, the right way to do it in PHP is unset()ing it. It's no hack
either; You might be used to setting an object to null in order to break
the reference, but it's not the right way to do it in PHP.
Zeev
On Sat, 12 Aug 2000, Simon Edwards wrote:
> 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
>
>
--
Zeev Suraski <zeev@zend.com>
http://www.zend.com/