3v4l.org

run code in 300+ PHP versions simultaneously
<?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(); ?>
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 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.0.5, 5.1.0 - 5.1.6, 5.2.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.3.11, 4.4.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

preferences:
167.21 ms | 1461 KiB | 7 Q