php - OOP maximum function nesting level -
i have weird situation oop
my class simple
class thing{ private $children = array(), $parent; public function addchild(self $thing){ $this->children[] = $thing; $thing->setparent($this); } public function setparent(self $thing){ $this->parent = $thing; $thing->addchild($this); } } $a = new thing(); $b = new thing(); $b->setparent($a);
if try use these functions maximum function nesting level of 100 error , know why, how supposed change code? way makes sense, if remove of function calls not work should.
as mentioned in comments, code creates infinite loop between setparent()
, addchild()
, wherein call setparent()
implicitly calls setchild()
, in turn calls setparent()
again.
if want code work such call setparent()
or call addchild()
enforces relationship in properties in both objects, can solve infinite loop experiencing adding if ()
condition inside addchild()
, calling setparent()
if object's parent not current object ($this
).
likewise, need check if object added child in addchild()
has not been added parent's $children
array using in_array()
.
class thing{ private $children = array(), $parent; // name property see results public $name; public function addchild(self $thing){ $this->children[] = $thing; // set object passed-in $thing object's // $parent property if has not been set: if ($thing->parent !== $this) { $thing->setparent($this); } } public function setparent(self $thing){ $this->parent = $thing; // add child via addchild() if not // in array if (!in_array($this, $thing->children)) { $thing->addchild($this); } } } $a = new thing(); $a->name = "thing a"; $b = new thing(); $b->name = "thing b"; // specify $a parent of $b $b->setparent($a); echo "\$a properties:\n"; print_r($a); echo "\$b properties:\n"; print_r($b);
the output above is:
$a properties: thing object ( [children:thing:private] => array ( [0] => thing object ( [children:thing:private] => array ( ) [parent:thing:private] => thing object *recursion* [name] => thing b ) ) [parent:thing:private] => [name] => thing ) $b properties: thing object ( [children:thing:private] => array ( ) [parent:thing:private] => thing object ( [children:thing:private] => array ( [0] => thing object *recursion* ) [parent:thing:private] => [name] => thing ) [name] => thing b )
now, using inverse action, starting $a
, $b
adding $b
child $a
instead of adding $a
parent of $b
:
$a = new thing(); $a->name = "thing a"; $b = new thing(); $b->name = "thing b"; // add $b child of $a $a->addchild($b);
produces same output:
$a properties: thing object ( [children:thing:private] => array ( [0] => thing object ( [children:thing:private] => array ( ) [parent:thing:private] => thing object *recursion* [name] => thing b ) ) [parent:thing:private] => [name] => thing ) $b properties: thing object ( [children:thing:private] => array ( ) [parent:thing:private] => thing object ( [children:thing:private] => array ( [0] => thing object *recursion* ) [parent:thing:private] => [name] => thing ) [name] => thing b )
(note "recursion" in print_r()
output: not indicate method calls behaving recursively, there recursive relationship between object references, want. $a
has child $b
, print_r()
attempts display parent of $b
, points $a
)
Comments
Post a Comment