note 10743 deleted from language.references.whatdo by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 01:20:37 +0000 |
| Subject: | note 10743 deleted from language.references.whatdo by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57326@lists.php.net to get a copy of this message | ||
Note Submitter: kluka@pobox.sk
----
There is an interesting feature (tested with PHP4.0.3pl1) of assignment of arrays and objects
containing references:
class C {
var $prop;
function C(&$x) { $this->prop = &$x; }
}
$a = 1;
$obj = new C($a);
$arr[1] = &$a;
$jbo = $obj; $rra = $arr;
$a++;
echo $obj->prop, $jbo->prop, $arr[1], $rra[1];
You should see 2222. Looks like assignment operator does some kind of shallow copy, stopping at
references. This prevents infinite loops when assigning cyclic data structures
class Cycle {
var $val; var $next;
function Cycle($v) { $this->val = $v; }
}
$a = new Cycle(1); $b = new Cycle(2);
$a->next = &$b; $b->next = &$a;
$c = $a;
var_dump($c); //Warning: Nesting level too deep - recursive dependency?
and makes $obj = new Class() where constructed object contains a reference and $arr =
array(...&$var...) work reasonably. However, list($b) = array(&$a) will NOT make $b a
reference to $a, and placing
&' before $b', array' or
list' would cause a parsing error.