I'm not in anyway a PHP expert, but from what I observed, it is combination of the parentheses and object operator (->
) that triggers the error. The assignment expression itself correctly returns an instance of DOMNodeList
, as we expect given the linked documentation :
$xml = "<person><name>John-Doe</name></person>";
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXpath($doc);
$test = ($x = $xpath->query("/person/foo"));
//$test contains instance of `DOMNodeList` on which we can use `->` to get the length
echo $test->length;
//but using `->` on parentheses triggers the initial error :
echo ($test)->length;
To achieve the actual goal, you maybe able to use plain for
loop, which provides more flexibility in controlling the loop, instead of using while
. Something along the lines of the following snippet :
for($x=$xpath->query($s);
$x->length;
$x=$xpath->query($s))
{
//do something with $x
}
I assume that $xpath->query($s)
result changes in the next iterations, in a way that $x->length
value decreases, so that the loop will eventually terminates.