note 107182 added to language.references
| From: | krauth dot spam1 at gmail dot com | Date: | Thu, 12 Jan 2012 21:19:38 +0000 |
| Subject: | note 107182 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185622@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.
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
----
Server IP: 69.147.83.197
Probable Submitter: 174.60.176.3
----
Manual Page -- http://www.php.net/manual/en/language.references.php
Edit -- https://master.php.net/note/edit/107182
Del: integrated -- https://master.php.net/note/delete/107182/integrated
Del: useless -- https://master.php.net/note/delete/107182/useless
Del: bad code -- https://master.php.net/note/delete/107182/bad+code
Del: spam -- https://master.php.net/note/delete/107182/spam
Del: non-english -- https://master.php.net/note/delete/107182/non-english
Del: in docs -- https://master.php.net/note/delete/107182/in+docs
Del: other reasons-- https://master.php.net/note/delete/107182
Reject -- https://master.php.net/note/reject/107182
Search -- https://master.php.net/manage/user-notes.php