PHP DOMDocument suddenly object of different class



I'm trying to return the root element of a DOMDocument ($doc->documentElement) and then to access the public member $foo of the ownerDocument. This gives me



Undefined property: DOMDocument::$foo in /var/www/temp/test.php on line 16


because after returning the root element the member ownerDocument is not any longer of class \test\DOMDocument but of \DOMDocument.


What's wrong with the code?


(PHP 5.5.9-1ubuntu4.5123)



<?php
namespace test;

class DOMDocument extends \DOMDocument {
public $foo = 'bar';
}

function test() {
$doc = new DOMDocument();
$doc->loadXML('<root></root>');
echo $doc->documentElement->ownerDocument->foo; // bar
return $doc->documentElement;
}

$doc = test();
echo $doc->ownerDocument->foo; // error: $foo is not defined

?>

No comments:

Post a Comment