note 107875 deleted from language.references by danbrown

From: Date: Thu, 10 May 2012 18:13:57 +0000
Subject: note 107875 deleted from language.references by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187738@lists.php.net to get a copy of this message
Note Submitter: LDRut ---- @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.

« previous php.notes (#187738) next »