Somewhere in my code I have:
class aclass {
...
function amethod() {
$this->dom = $a_dom_document;
$this->about = array('an_element' => $an_element_of_that_document);
}
...
}
/* Somewhere else */
$instance->dom; // It's there, no problem.
$instance->about['an_element']->parentNode->replaceChild($something_else, $this->about['an_element']);
The code is complicated; I'm trying to only give the gist, here.
The strange thing is that it works about a quarter of the time. 3 out of 4 times, PHP says that replaceChild is a "Call to a member function replaceChild() on a non-object", but a quarter of the time, it actually works. What could be going on?
Edit: the following
print_r($instance->about['an_element']);
print_r($instance->about['an_element']->parentNode);
print method_exists($instance->about['an_element'], 'replaceChild')?'exists':'does not exist');
print_r($something_else);
returns:
DOMElement Object
(
)
DOMElement Object
(
)
exists
DOMElement Object
(
)
This is true even when the page fails.
I must be missing something very obvious. The $something_else is a node of the same DOM document.
SOLUTION: It was indeed very simple: for reasons I still don't quite understand, that part of the code is called twice. In one case, the instance is not defined, but that instance shows up in second place in the logs and I really was only looking for one. An if took care of it. Now I have to see why the #@!~ there are two of them in the first place.