<?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();
?>
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.
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.
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