|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52858 dom_import_simplexml() doesn't work on newly created SimpleXMLElement nodes
Submitted: 2010-09-16 05:51 UTC Modified: 2018-05-05 20:40 UTC
Avg. Score:3.8 ± 1.5
Reproduced:4 of 5 (80.0%)
Same Version:2 (50.0%)
Same OS:4 (100.0%)
From: tatarynowicz at gmail dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.3.3 OS: Windows 7
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: tatarynowicz at gmail dot com
New email:
PHP Version: OS:


 [2010-09-16 05:51 UTC] tatarynowicz at gmail dot com
The problem is readily apparent when you run the test script. When 
SimpleXMLElement dynamically creates a child element, and you directly call a 
method on the child element, the $this context is apparently not a proper 
SimpleXMLElement, i.e. dom_import_simplexml() does not accept it as input.

class MyXML extends SimpleXMLElement {
    public function cdata($text) {

$xml = new MyXML('<foo/>');

Test script:

class MyXML extends SimpleXMLElement {
    public function cdata($text) {
        $node = dom_import_simplexml($this);
        $owner = $node->ownerDocument;
        return $this;

$xml = new MyXML('<foo/>');

// works
$xml->one = 'One';

// also works
$xml->two = '';

// doesn't work

print $xml->asXML();

Expected result:
<?xml version="1.0"?>

Actual result:
Warning: dom_import_simplexml(): Invalid Nodetype to import in 
C:\WWW\Work\WC\www\dom.php on line 5

Notice: Trying to get property of non-object in C:\WWW\Work\WC\www\dom.php on line 

Fatal error: Call to a member function appendChild() on a non-object in 
C:\WWW\Work\WC\www\dom.php on line 7


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-13 11:58 UTC] hanskrentel at yahoo dot de
Please compare what you do with:


You will get a warning:

Warning: SimpleXMLElement::addChild(): Cannot add child. Parent is not a permanent member of the XML tree

If you extend from SompleXMLElement you probably want to add a similar warning for your function. You can check for the condition this way:

    if ($this[0] == NULL) {
        $where = __CLASS__ . '::' . __FUNCTION__ . '(): ';
        $what  = 'Cannot add child. Parent is not a permanent member of the XML tree';
        trigger_error($where . $what, E_USER_WARNING);

I hope this is helpful and makes the bigger picture more clear that this is how SimpleXMLElement works.

You can however create your own type that does know how to add this on the fly, however due to the magic nature of SimpleXMLElement, it is not possible as long as you extend from 
It requires you to create a decorator and write code for that part of the magic your own and delegate the rest to the SimpleXMLElement decoratee / subject.
 [2018-05-05 20:40 UTC]
-Status: Open +Status: Not a bug
 [2018-05-05 20:40 UTC]
As hinted at, ->three does not exist in the XML but SimpleXML will give something to pretend that it does - perhaps it could sometimes exist in the document but the one it parsed did not contain one. SimpleXML cannot actually create a <three> because that would necessarily modify the document... which in this case is wanted, yes, but in the general case should not happen.

It would be nice if MyXML could add detached copies of itself to the intended document, but being separated from the document means it can't do that, so since SimpleXMLElement doesn't use magic methods like __get that means @hanskrentel's advice is the only recourse I can see.

Closing NAB also due to age.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 24 15:01:29 2024 UTC