Edit report at https://bugs.php.net/bug.php?id=62639&edit=1
ID: 62639 Comment by: sergey at shymko dot net Reported by: alexshock at yandex dot ru Summary: XML structure broken Status: Assigned Type: Bug Package: SimpleXML related Operating System: debian 6.0.5 PHP Version: 5.4.5 Assigned To: acurioso Block user comment: N Private report: N New Comment: The following workaround seems to fix both dumping and type casting: <?php class SimpleXMLElement_CloneFix extends SimpleXMLElement { public function xpath($path) { $result = parent::xpath($path) ?: array(); foreach ($result as $node) { $this->_fixNode($node); } return $result; } protected function _fixNode(SimpleXMLElement $node) { if (!$node && (string)$node) { $dom = dom_import_simplexml($node); $dom->parentNode->replaceChild(clone $dom, $dom); } } } Unfortunately, the same approach does not work for children() method, because of mysterious nature of its returned value. Despite returned object manifests itself as an instance of SimpleXMLElement, it does not behave as such. For instance, it is incompatible with dom_import_simplexml(). So, what kind of SimpleXMLElement is that? Previous Comments: ------------------------------------------------------------------------ [2013-06-14 00:21:27] info at fuktommy dot com It causes not only wrong var_dump and print_r, but also wrong boolean cast. Test script: ------------ $xml = <<<XML <?xml version="1.0"?> <a> <b>Some String</b> </a> XML; $b = simplexml_load_string($xml)->xpath('/a/b'); var_dump((string)$b[0]); var_dump((bool)$b[0]); Expected result: ---------------- string(11) "Some String" bool(true) Actual result: -------------- string(11) "Some String" bool(false) ------------------------------------------------------------------------ [2013-04-24 07:48:36] larue...@php.net I think we should revert the previous fix at now, since this new bug is more critical than the previous one. and I think the key problem there is that we can not tell iterating from convert_to_array in the sxe_get_prop_hash without that, I don't think we can get a good fix for the 034.phpt ------------------------------------------------------------------------ [2013-02-18 21:21:48] sala...@php.net Related To: Bug #62203 ------------------------------------------------------------------------ [2012-07-30 01:57:05] willfi...@php.net I think that would make more sense. Thanks, Andrew. ------------------------------------------------------------------------ [2012-07-30 00:58:13] acuri...@php.net I can confirm that reverting that patch does fix the bug; however, it causes the original test to fail again: ===================================================================== FAILED TEST SUMMARY --------------------------------------------------------------------- SimpleXML: cast to array [ext/simplexml/tests/034.phpt] Bug #51615 (PHP crash with wrong HTML in SimpleXML) [ext/simplexml/tests/bug51615.phpt] ===================================================================== See: ext/simplexml/tests/034.phpt for more information. Ignore bug51615.phpt for now. That is just a side effect of test 34. I would actually prefer if I could take the first stab at this bug myself since it was clearly introduced in my code. I'd be hesitate to just revert the changes since that would break test 34. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=62639 -- Edit this bug report at https://bugs.php.net/bug.php?id=62639&edit=1