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; } } // 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(); ?>
Output for git.master, git.master_jit, rfc.property-hooks
Warning: Undefined variable $bar1 in /in/cCL24 on line 26 Fatal error: Uncaught Error: Call to a member function setName() on null in /in/cCL24:26 Stack trace: #0 {main} thrown in /in/cCL24 on line 26
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
35.61 ms | 401 KiB | 8 Q