Re: Re: destroying objects in an array in PHP5
| From: | Matthew Sims | Date: | Tue, 27 Jul 2004 20:49:49 +0000 |
| Subject: | Re: Re: destroying objects in an array in PHP5 | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-192015@lists.php.net to get a copy of this message | ||
>
> This is not true. PHP 5 passes objects by reference to functions
> only. Assignments are treated the same way as php4 externally.
>
> <?php
>
> /* note: no &$f */
> function testfoo($f) {
> $f->foo = 'testfoo';
> }
>
> class foo { public $foo; }
>
> /* object is passed by reference */
> $a = new foo();
> $a->foo = 'main';
> testfoo($a);
> var_dump($a); /* ->foo == 'testfoo' */
>
> /* variable is only copied */
> $b = $a;
> $a = null;
> var_dump($a); /* NULL */
> var_dump($b); /* Object 1 */
>
> /* make 2 reference but remove one of them */
> $a = new foo();
> $b = &$a;
> unset($a); /* only removes the reference to object #1 */
> var_dump($a); /* Undefined */
> var_dump($b); /* Object #1 */
>
>
> /* here is your solution */
> /* make 2 references but delete object, 2 variable
> * references still exits */
> $a = new foo();
> $b = &$a;
> $a = null;
> var_dump($a); /* NULL */
> var_dump($b); /* NULL */
>
> /* proof that references still exists */
> $b = new foo();
> var_dump($a); /* Object #1 */
> var_dump($b); /* Object #1 */
>
>
> Curt
Wow! I just learned a whole lotta OO from just this email. Thanks Curt!
--
--Matthew Sims
--<http://killermookie.org>