Problem with dom_import_simplexml on newly created SimpleXML nodes

aves

New Member
I'm working on a class extending \[code\]SimpleXMLElement\[/code\]:\[code\]class MyXML extends SimpleXMLElement { public function cdata($text) { $node = dom_import_simplexml($this); $owner = $node->ownerDocument; $node->appendChild($owner->createCDATASection($text)); return $this; }}\[/code\]Since it's an \[code\]SimpleXMLElement\[/code\], I can dynamically create XML nodes inside it:\[code\]$xml = new MyXML('<foo/>');$xml->bar = 'Test';print $xml->asXML(); // <foo><bar>Test</bar></foo>\[/code\]But when I try to run this:\[code\]$xml = new MyXML('<foo/>');$xml->bar->cdata('Test');\[/code\]I get:\[code\]Warning: dom_import_simplexml(): Invalid Nodetype to import in [..]\[/code\]However, if I force the \[code\]SimpleXMLElement\[/code\] node to be created before running \[code\]cdata()\[/code\], it works again:\[code\]$xml = new MyXML('<foo/>');$xml->bar = '';$xml->bar->cdata('Test');print $xml->asXML(); // <foo><bar><![CDATA[Test]]></bar></foo>\[/code\]I'm curious if what I found is a bug, and if there is any way to work around it without "priming" the node first.
 
Back
Top