3v4l.org

run code in 200+ php & hhvm versions
<?php class Foo { function Foo($name) { // create a reference inside the global array $globalref global $globalref;//<--- vari├ível global WTF que tenta exemplicar a diferen├ža //do new e do &= $globalref[] = &$this; // set name to passed value $this->setName($name); // and put it out $this->echoName(); } function echoName() { echo "<br />", $this->name; } function setName($name) { $this->name = $name; } } $bar1 = new Foo('set in constructor'); $bar1->echoName(); $globalref[0]->echoName(); $bar2 =& new Foo('set in constructor'); $bar2->echoName(); $globalref[1]->echoName(); // now we will change the name. what do you expect? // you could expect that both $bar1 and $globalref[0] change their names... $bar1->setName('set from outside'); // as mentioned before this is not the case. $bar1->echoName(); $globalref[0]->echoName(); /* output: set from outside set in constructor */ // let us see what is different with $bar2 and $globalref[1] $bar2->setName('set from outside'); // luckily they are not only equal, they are the same variable // thus $bar2->name and $globalref[1]->name are the same too $bar2->echoName(); $globalref[1]->echoName(); $bar1->setName('set from outside'); // as mentioned before this is not the case. $bar1->echoName(); $globalref[0]->echoName(); /* output: set from outside set in constructor */ // let us see what is different with $bar2 and $globalref[1] $bar2->setName('set from outside'); // luckily they are not only equal, they are the same variable // thus $bar2->name and $globalref[1]->name are the same too $bar2->echoName(); $globalref[1]->echoName(); ?>
based on cCL24
Output for 5.3.0 - 5.6.28
Deprecated: Assigning the return value of new by reference is deprecated in /in/bgBVS on line 27 <br />set in constructor<br />set in constructor<br />set in constructor<br />set in constructor<br />set in constructor<br />set in constructor<br />set from outside<br />set from outside<br />set from outside<br />set from outside<br />set from outside<br />set from outside<br />set from outside<br />set from outside
Output for 5.0.0 - 5.2.17
Strict Standards: Assigning the return value of new by reference is deprecated in /in/bgBVS on line 27 <br />set in constructor<br />set in constructor<br />set in constructor<br />set in constructor<br />set in constructor<br />set in constructor<br />set from outside<br />set from outside<br />set from outside<br />set from outside<br />set from outside<br />set from outside<br />set from outside<br />set from outside
Output for 4.3.0 - 4.4.9
<br />set in constructor<br />set in constructor<br />set in constructor<br />set in constructor<br />set in constructor<br />set in constructor<br />set from outside<br />set in constructor<br />set from outside<br />set from outside<br />set from outside<br />set in constructor<br />set from outside<br />set from outside