@@ -83,11 +83,14 @@ public function __get($key)
8383 }
8484
8585 /**
86- * @todo Remove the need for this to be called .
86+ * Attempts to clear out any object references .
8787 */
8888 public function __destruct ()
8989 {
90- $ this ->clear ();
90+ $ this ->tag = null ;
91+ $ this ->attr = [];
92+ $ this ->parent = null ;
93+ $ this ->children = [];
9194 }
9295
9396 /**
@@ -150,6 +153,9 @@ public function setParent(Node $parent)
150153 // assign child to parent
151154 $ this ->parent ->addChild ($ this );
152155
156+ //clear any cache
157+ $ this ->clear ();
158+
153159 return $ this ;
154160 }
155161
@@ -243,6 +249,9 @@ public function addChild(Node $child)
243249 // tell child I am the new parent
244250 $ child ->setParent ($ this );
245251
252+ //clear any cache
253+ $ this ->clear ();
254+
246255 return true ;
247256 }
248257
@@ -272,6 +281,9 @@ public function removeChild($id)
272281 // remove the child
273282 unset($ this ->children [$ id ]);
274283
284+ //clear any cache
285+ $ this ->clear ();
286+
275287 return $ this ;
276288 }
277289
@@ -351,6 +363,26 @@ public function isAncestor($id)
351363 return false ;
352364 }
353365
366+ /**
367+ * Attempts to get an ancestor node by the given id.
368+ *
369+ * @param int $id
370+ * @return null|Node
371+ */
372+ public function getAncestor ($ id )
373+ {
374+ if ( ! is_null ($ this ->parent ))
375+ {
376+ if ($ this ->parent ->id () == $ id )
377+ {
378+ return $ this ->parent ;
379+ }
380+ return $ this ->parent ->getAncestor ($ id );
381+ }
382+
383+ return null ;
384+ }
385+
354386 /**
355387 * Shortcut to return the first child.
356388 *
@@ -589,16 +621,9 @@ public function get_display_size()
589621 return $ result ;
590622 }
591623
592- /**
593- * clean up memory due to php5 circular references memory leak...
594- *
595- * @todo Remove the need for this. (Remove circular references)
596- */
597- protected function clear ()
598- {
599- $ this ->nodes = null ;
600- $ this ->parent = null ;
601- $ this ->children = null ;
602- }
603-
624+ /**
625+ * Call this when something in the node tree has changed. Like a child has been added
626+ * or a parent has been changed.
627+ */
628+ protected function clear () {}
604629}
0 commit comments