Re: Re: SimpleXML & libxml options (XInclude)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ben Roberts wrote:
So really when you perform:

  $dom = dom_import_simplexml($xml);

the $dom object is really created by taking a reference to the $xml object rather than copying it? i.e. (I know this isn't real code)

  $dom = dom_import_simplexml(& $xml);
Not exactly, but the idea is along those lines.
What is happening is that $dom (DOMElement in this case) now points to the same libxml2 structure as does $xml (SimpleXMLElement). So now when you modify the structure of the document with one, it is reflected by the other because they are in the same document. This also means that it is possible that objects can become invalid by changes made in the other extension.

$sxe = new SimpleXMLElement("<root><child><inner>text</inner></child></root>");
$sxechild = $sxe->child->inner[0];

$dom = dom_import_simplexml($sxe);
$dom->removeChild($dom->firstChild);

var_dump($sxechild);

PHP Warning: var_dump(): Node no longer exists in /home/rrichards/temp.php on line 8
object(SimpleXMLElement)#3 (0) {
}
This shows that $sxechild is still an SimpleXMLElement, but warnings are issues that the node it refers to in the document no longer exists. Although I doubt someone use the code above, it is the smallest piece of code I could write to demonstrate the possibility.

Rob

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux