note 56135 deleted from language.references.whatdo by colder
| From: | colder@php.net | Date: | Sat, 24 Feb 2007 11:49:57 +0000 |
| Subject: | note 56135 deleted from language.references.whatdo by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-123829@lists.php.net to get a copy of this message | ||
Note Submitter: chat~kaptain524 at neverbox dot com
----
Example 21-3 says that using references in "complex arrays" may not work as expected. But
I have found a way around it, to get normal reference behavior.
Compare the output of these two scripts:
<?php
for ( $i = 0; $i < 10; $i ++ ) $top[$i] = &$top;
$top['A'] = 'AAAAA'; var_dump( $top );
?>
<?php
$z = &$top;
for ( $i = 0; $i < 10; $i ++ ) $top[$i] = &$top;
$top['A'] = 'AAAAA'; var_dump( $top );
?>
Running PHP 5.0, the first generates about 157457 lines while the second only generates 2554 lines.
The only difference in the code is setting an initial reference before adding self-references inside
the array. This simple step prevents the array from being duplicated every time another
self-reference is added.
I am not sure if the duplication is intentional or if it's a bug in the way references are
created, but the docs certainly recognize it.