note 30047 added to language.references

From: Date: Wed, 05 Mar 2003 10:12:01 +0000
Subject: note 30047 added to language.references
Groups: php.notes 
Request: Send a blank email to php-notes+get-44854@lists.php.net to get a copy of this message
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> -- http://www.php.net/manual/en/language.references.php http://master.php.net/manage/user-notes.php?action=edit+30047 http://master.php.net/manage/user-notes.php?action=delete+30047 http://master.php.net/manage/user-notes.php?action=reject+30047

« previous php.notes (#44854) next »