note 107875 added to language.references

From: Date: Mon, 12 Mar 2012 19:11:33 +0000
Subject: note 107875 added to language.references
Groups: php.notes 
Request: Send a blank email to php-notes+get-186612@lists.php.net to get a copy of this message
@krauth, you somewhat mistate what is going on. In the PHP 5.3 version, each element of $objs is assigned a reference to the value of $a. The do, and should, always have the exact same value. Every time the value of $a is overwritten, each and every element of $objs assumes the new value. In the PHP 4 version you are assigning a reference to a reference. This is unrolled so that $a is removed from the chain - that is the value of $objs[] is not a reference to $a which is a reference to the new expression, it is instead a direct reference to the value of the new expression at the time of the assignment. In PHP 5.3 ALL objects are passed by reference so neither ampersand has any legitimate need any longer. The correct edit is to remove BOTH ampersands. <?php echo "new correct PHP 5 style, ** removal of & in 'new' assignment and array assignment**:\n"; $objs = array(); foreach (array(37, "Wisconsin", 3.14159) as $i) { $a = new Gotcha($i); $objs[] = $a; } ?> This is the exact semantic equivalent to your old 4.3 code. ---- Server IP: 69.147.83.197 Probable Submitter: 75.132.154.20 ---- Manual Page -- http://www.php.net/manual/en/language.references.php Edit -- https://master.php.net/note/edit/107875 Del: integrated -- https://master.php.net/note/delete/107875/integrated Del: useless -- https://master.php.net/note/delete/107875/useless Del: bad code -- https://master.php.net/note/delete/107875/bad+code Del: spam -- https://master.php.net/note/delete/107875/spam Del: non-english -- https://master.php.net/note/delete/107875/non-english Del: in docs -- https://master.php.net/note/delete/107875/in+docs Del: other reasons-- https://master.php.net/note/delete/107875 Reject -- https://master.php.net/note/reject/107875 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186612) next »