note 107182 modified in language.references by danbrown

From: Date: Sat, 14 Jan 2012 16:25:26 +0000
Subject: note 107182 modified in language.references by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-185659@lists.php.net to get a copy of this message
Here's a simple demonstration that "$var =& new ..." and "$var = new ..." are NOT equivalent, as suggested in several places. We came across this when trying to fix the PHP 5.3 deprecation notice "Assigning the return value of new by reference is deprecated". Simply removing the ampersand actually broke the code, and in fact it seems that the statment with the ampersand is the one that does not create a reference. Very confusing. <?php class Gotcha { var $x; public function Gotcha($init_value) { $this->x = $init_value; } } echo "<PRE>\n\n"; echo "old PHP 4 style, generates 'deprecated' warnings:\n"; $objs = array(); foreach (array(37, "Wisconsin", 3.14159) as $i) { $a =& new Gotcha($i); $objs[] =& $a; } var_export($objs); echo "\n\n"; echo "new PHP 5 style, ** only change is removal of & in 'new' assignment **:\n"; $objs = array(); foreach (array(37, "Wisconsin", 3.14159) as $i) { $a = new Gotcha($i); $objs[] =& $a; } var_export($objs); echo "\n\n"; echo "php version = " . phpversion() . "\n"; ?> Output is: old PHP 4 style, generates 'deprecated' warnings: array ( 0 => Gotcha::__set_state(array( 'x' => 37, )), 1 => Gotcha::__set_state(array( 'x' => 'Wisconsin', )), 2 => Gotcha::__set_state(array( 'x' => 3.14159, )), ) new PHP 5 style, ** only change is removal of & in 'new' assignment **: array ( 0 => Gotcha::__set_state(array( 'x' => 3.14159, )), 1 => Gotcha::__set_state(array( 'x' => 3.14159, )), 2 => Gotcha::__set_state(array( 'x' => 3.14159, )), ) php version = 5.3.3-7+squeeze3 --was-- Here's a simple demonstration that "$var =& new ..." and "$var = new ..." are NOT equivalent, as suggested in several places. We came across this when trying to fix the PHP 5.3 deprecation notice "Assigning the return value of new by reference is deprecated". Simply removing the ampersand actually broke the code, and in fact it seems that the statment with the ampersand is the one that does not create a reference. Very confusing. Thanks to the anonymous poster whose note on 01-Sep-2009 08:07 was the tip off to this problem, and truly did save us hours of head scratching! <?php class Gotcha { var $x; public function Gotcha($init_value) { $this->x = $init_value; } } echo "<PRE>\n\n"; echo "old PHP 4 style, generates 'deprecated' warnings:\n"; $objs = array(); foreach (array(37, "Wisconsin", 3.14159) as $i) { $a =& new Gotcha($i); $objs[] =& $a; } var_export($objs); echo "\n\n"; echo "new PHP 5 style, ** only change is removal of & in 'new' assignment **:\n"; $objs = array(); foreach (array(37, "Wisconsin", 3.14159) as $i) { $a = new Gotcha($i); $objs[] =& $a; } var_export($objs); echo "\n\n"; echo "php version = " . phpversion() . "\n"; ?> Output is: old PHP 4 style, generates 'deprecated' warnings: array ( 0 => Gotcha::__set_state(array( 'x' => 37, )), 1 => Gotcha::__set_state(array( 'x' => 'Wisconsin', )), 2 => Gotcha::__set_state(array( 'x' => 3.14159, )), ) new PHP 5 style, ** only change is removal of & in 'new' assignment **: array ( 0 => Gotcha::__set_state(array( 'x' => 3.14159, )), 1 => Gotcha::__set_state(array( 'x' => 3.14159, )), 2 => Gotcha::__set_state(array( 'x' => 3.14159, )), ) php version = 5.3.3-7+squeeze3 http://php.net/manual/en/language.references.php

« previous php.notes (#185659) next »