note 66899 deleted from language.oop.newref by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 19:11:08 +0000 |
| Subject: | note 66899 deleted from language.oop.newref by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162582@lists.php.net to get a copy of this message | ||
Note Submitter: aib
----
Note that the "new" operator no longer returns a copy of the constructed object, but the
object itself. This behavior may be as of PHP 5.x.
This means that
$foo = new class();
and
$foo =& new class();
are equivalent now, and you don't have to worry about the problems stated on this page.
Consider the following example:
<?php
class foo
{
var $num;
function foo()
{
global $globalref;
$globalref[] = &$this;
}
}
$bar = new foo();
$bar->num = 1;
$globalref[0]->num = 2;
echo "\$bar->num = ".$bar->num."\n";
echo "\$globalref[0]->num = ".$globalref[0]->num."\n";
?>
On PHP 5.0.1, it will print:
$bar->num = 2
$globalref[0]->num = 2
Whereas on PHP 4.4.0, it would print:
$bar->num = 1
$globalref[0]->num = 2
unless you assigned $bar to a new foo using a reference.