If I use a tree structure of nodes similar to the code below, do I need to worry about the circular link?
I read that PHP uses a memory allocation mechanism that can make life very difficult for the garbage collector when there are round links.
What I want to know:
- If my tree consists of only a few nodes, say 25, is this a problem?
- Will memory be freed at the end of the script or am I slowly creating a problem for the server?
- Under what circumstances will this problem have an effect during script execution?
- Manually deleting links solves the problem, and should I always do this?
class Node {
private $parent;
private $children;
function addChild( Node $child ) {
$this->children[] = $child;
$child->setParent( $this );
}
function setParent( $parent ) {
$this->parent = $parent;
}
}
$node0 = new Node;
$node1 = new Node;
$node0->addChild( $node1 );