php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33450 DOMNodes forget their class
Submitted: 2005-06-23 11:36 UTC Modified: 2005-06-23 12:40 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: arendjr at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.4 OS: Linux
Private report: No CVE-ID: None
 [2005-06-23 11:36 UTC] arendjr at gmail dot com
Description:
------------
Whenever I create a class which inherits DOMElement, and I create an instance of this class, the type of the class is "forgotten" when the variable holding the instance goes out of scope, even though the actual instance should still exist as it is part of the DOM tree. So, what you get is that the inherited DOMElement still exists in the DOM tree, but the subclass instance is gone.



Reproduce code:
---------------
<?php

class Widget extends DOMElement {
	public function __construct() {
		parent::__construct('p');
	}

	public function say() {
		echo "I am a widget!\n";
	}
}

$doc = new DOMDocument();
$doc->appendChild($html = new DOMElement('html'));
$html->appendChild($body = new DOMElement('body'));
$body->appendChild(new Widget());
$body->firstChild->say();

?>

Expected result:
----------------
It should say: "I am a widget!".

You can get this expected result by changing the line:
  $body->appendChild(new Widget());
to:
  $body->appendChild($widget = new Widget());

this way, the widget will not get out of scope and the actual class is remembered.

Actual result:
--------------
You will get an error saying: "Fatal error: Call to undefined method DOMElement::say() in testcase.php on line 17"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-23 11:41 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-06-23 11:57 UTC] arendjr at gmail dot com
Just tried it, but it gives exactly the same result.
 [2005-06-23 12:40 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This has been covered many. Its all due to scope as your call creates the object and its immediately destroyed since its never returned. The tree does NOT persists objects (variables) that have been destroyed.
Use $widget = $body->appendChild(new Widget());
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Aug 08 16:00:03 2025 UTC