I have a ParentClass and when I make a new object I want to pass a reference to the ParentClass. (I have to use the ParentClass things in the new object)
I use the constructor to make this object and pass the reference value. (that's important for me)
But when I use the =&
operator, it makes a new instance of the ParentClass, what call the constructor, and then it's fall an endless recursion.
Here's my code:
<?php
abstract class MainClass {
function __construct(&$parent){
$this->parent =& $parent;
$this->init();
}
abstract protected function init();
}
class ParentClass extends MainClass {
protected function init(){
$this->child = new ChildClass($this);
}
}
class ChildClass extends MainClass {
protected function init(){}
}
$parent = new ParentClass (new stdClass());
var_dump($parent);
?>
And the result:
object(ParentClass)#1 (2) {
["parent"]=>
object(stdClass)#2 (0) {
}
["child"]=>
object(ChildClass)#3 (1) {
["parent"]=>
object(ParentClass)#1 (2) {
["parent"]=>
object(stdClass)#2 (0) {
}
["child"]=>
object(ChildClass)#3 (1) {
["parent"]=>
*RECURSION*
}
}
}
}
How can I solve this problem?