Re: Re: destroying objects in an array in PHP5
| From: | Curt Zirzow | Date: | Tue, 27 Jul 2004 20:54:34 +0000 |
| Subject: | Re: Re: destroying objects in an array in PHP5 | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-192014@lists.php.net to get a copy of this message | ||
* Thus wrote Erik Franzn:
> $oObjectB[$i] = &$oObjectA[$i];
>
> The above statement does not matter, because in PHP5 "objects are
> referenced by handle, and not by value".
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
--
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about. No, sir. Our model is the trapezoid!