/** * Remove a child node * * This function removes a child from the children array. A parameter * tells the function whether to destroy the child afterwards or not. * If the specified node is not a child of this node, the function will * return false. * * @access public * @param mixed $child The child to destroy; either an integer * specifying the index of the child or a reference * to the child itself. * @param bool $destroy Destroy the child afterwards. * @return bool On success, the function returns true, else false. */ function removeChild(&$child, $destroy = false) { if (is_object($child)) { // if object: get index $object =& $child; unset($child); $child = $this->_findChild($object); if ($child === false) { return false; } } else { // remove reference on $child $save = $child; unset($child); $child = $save; // else: get object if (!isset($this->_children[$child])) { return false; } $object =& $this->_children[$child]; } // store count for later use $ccount = count($this->_children); // index out of bounds if (!is_int($child) || $child < 0 || $child >= $ccount) { return false; } // inkonsistency if ($this->_children[$child]->_parent === null || $this->_children[$child]->_parent->_id != $this->_id) { return false; } // $object->_parent = null would equal to $this = null // as $object->_parent is a reference to $this! // because of this, we have to unset the variable to remove // the reference and then redeclare the variable unset($object->_parent); $object->_parent = null; // we have to unset it because else it will be overridden in // in the loop unset($this->_children[$child]); // move all remaining objects one index higher while ($child < $ccount - 1) { // save object $obj =& $this->_children[$child + 1]; // we have to unset it because else it will be // overridden in in the loop unset($this->_children[$child + 1]); // put object to new position $this->_children[$child] =& $obj; // UNSET THE OBJECT! unset($obj); $child++; } if ($destroy) { return Parser_Node::destroyNode($object); unset($object); } return true; }