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 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
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.
Output for 7.0.5 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Foo has a deprecated constructor in /in/cCL24 on line 2 Notice: 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.
Output for 7.3.32 - 7.3.33
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.
Output for 7.0.0 - 7.0.4
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Foo has a deprecated constructor in /in/cCL24 on line 2 Notice: Undefined variable: bar1 in /in/cCL24 on line 26 Fatal error: Uncaught Error: Call to a member function setName() on unknown in /in/cCL24:26 Stack trace: #0 {main} thrown in /in/cCL24 on line 26
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Notice: Undefined variable: bar1 in /in/cCL24 on line 26 Fatal error: Call to a member function setName() on null in /in/cCL24 on line 26
Process exited with code 255.
Output for 5.0.4 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38
Notice: Undefined variable: bar1 in /in/cCL24 on line 26 Fatal error: Call to a member function setName() on a non-object in /in/cCL24 on line 26
Process exited with code 255.
Output for 5.0.0 - 5.0.3
Notice: Undefined variable: bar1 in /in/cCL24 on line 26 Fatal error: Call to a member function setName() on a non-object in /in/cCL24 on line 26
Process exited with code 255.
Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.9
Fatal error: Call to a member function on a non-object in /in/cCL24 on line 26
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Call to a member function on a non-object in /in/cCL24 on line 26

preferences:
350.44 ms | 401 KiB | 459 Q