The code:
<?
class foo {
var $bar;
function foo() {
$GLOBALS["secondobject"] = &$this;
if (!$this->bar)
$this->bar = "Bleh bleh blah";
}
}
$firstobject = new foo();
$secondobject->bar .= " buh";
print "<hr><pre>";
var_dump($firstobject, "firstobject");
print "</pre><hr><pre>";
var_dump($secondobject, "secondobject");
print "</pre><hr>";
?>
Produces:
object(foo)(1) {
["bar"]=>
string(14) "Bleh bleh blah"
}
string(11) "firstobject"
object(foo)(1) {
["bar"]=>
string(18) "Bleh bleh blah buh"
}
string(12) "secondobject"
While the code:
<?
class foo {
var $bar;
function foo() {
$GLOBALS["secondobject"] = &$this;
if (!$this->bar)
$this->bar = "Bleh bleh blah";
}
}
$firstobject = new foo();
$firstobject->foo();
$secondobject->bar .= " buh";
print "<hr><pre>";
var_dump($firstobject, "firstobject");
print "</pre><hr><pre>";
var_dump($secondobject, "secondobject");
print "</pre><hr>";
?>
produces:
object(foo)(1) {
["bar"]=>
string(18) "Bleh bleh blah buh"
}
string(11) "firstobject"
object(foo)(1) {
["bar"]=>
string(18) "Bleh bleh blah buh"
}
string(12) "secondobject"
Why doesn't the constructor get runned properly at object creation...
I have to call the constructor explicitly for the reference to get properly set up...
Is this a bug? Or is it a feature? 🙂
Does it have something to do about the object not beeing properly set up when the constructor is called the first time... so that the reference to $secondobject does not get the way I want it...