note 30047 deleted from language.references by sniper

From: Date: Tue, 30 Sep 2003 01:18:45 +0000
Subject: note 30047 deleted from language.references by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57315@lists.php.net to get a copy of this message
Note Submitter: igjav@cesga.es ---- I've made this testcase to show a case of "self referencing" applicated to arrays. It seems to point out that must be taken with care, but only in terms of when is possible to reference. It's based on comments over: http://bugs.php.net/bug.php?id=17959 Sorry for the careless sense of key names.... <? $top = array ( 'name' => 'top', 'parent' => null, 'A' => array ( 'name' => 'A', 'element1' => &$top, 'parent' => &$top['name'], ), ); $top2 = array ( 'name' => 'top2', 'parent' => null, 'A' => array ( 'name' => 'A', 'element1' => &$top, 'parent' => &$top['name'], ), ); ?> <pre> <strong><font color=red> $top = array ( 'name' => 'top', 'parent' => null, 'A' => array ( 'name' => 'A', 'element1' => &$top, 'parent' => &$top['name'], ), ); $top2 = array ( 'name' => 'top2', 'parent' => null, 'A' => array ( 'name' => 'A', 'element1' => &$top, 'parent' => &$top['name'], ), ); </font></strong> top['A']: <? foreach ( $top['A'] as $k => $v ) { printf("$k =&gt; $v\n"); } // // What happened? // // Seems you cannot reference an array element before creating "completely" the array ?> This seems to be "completely" safe: top2['A']: <? foreach ( $top2['A'] as $k => $v ) { printf("$k =&gt; $v\n"); } ?> Then more reference tests: <strong><font color=red> $top['A']['parent'] = $top['name']; echo($top['A']['parent']); </font></strong><br> <? $top['A']['parent'] = $top['name']; echo($top['A']['parent']); ?> <strong><font color=red> $refTop = &$top; $top['A']['parent'] = $refTop['name']; echo($top['A']['parent']); </font></strong><br> <? $refTop = &$top; $top['A']['parent'] = $refTop['name']; echo($top['A']['parent']); ?> <strong><font color=red> $top['A']['parent'] = &$top['name']; echo($top['A']['parent']); </font></strong><br> <? $top['A']['parent'] = &$top['name']; echo($top['A']['parent']); ?> <strong><font color=red> echo($top['A']['element1']['name']); </font></strong><br> <? echo($top['A']['element1']['name']); ?> <strong><font color=red> echo($top['A']['element1']['A']['parent']); </font></strong><br> <? echo($top['A']['element1']['A']['parent']); ?> </pre>

« previous php.notes (#57315) next »