php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30321 serializing a DOMDocument object doesn't work
Submitted: 2004-10-04 15:32 UTC Modified: 2004-10-04 16:53 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: matthias dot hoseit at comline dot de Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.0.2 OS: Windows 2000, Apache 1.3.31
Private report: No CVE-ID: None
 [2004-10-04 15:32 UTC] matthias dot hoseit at comline dot de
Description:
------------
The function serialize() doesn't work with a DOMDocument object. After you've serialized and unserialized an object of DOMDocument the unserialized object seems to be empty (The object knows that it is a DOMDocument Object, but all data is lost).

Reproduce code:
---------------
<?PHP
	//----------------------------------------------Building a working DOMDocument-----------------------------------
	//create new DOMDocument
	$domDoc1 = new DOMDocument();
	//create some elements
	$rootElement = $domDoc1->createElement("Element1");
	$element2 = $domDoc1->createElement("Element2", "Value2");
	//adding element to root
	$rootElement->appendChild($element2);
	//adding root to DOM
	$domDoc1->appendChild($rootElement);
	//show that DOMDocument is working right now
	$elements1 = $domDoc1->getElementsByTagName("Element2");
	foreach ($elements1 as $val) {
	        echo "TagName = '".$val->tagName."' ";
			echo "Value = '".$val->nodeValue."'<br>";
	}
	//----------------------------------------------now the strange behavior-----------------------------------------
	//now serialize DOMObject
	$serialize1 = serialize($domDoc1);
	//The serialized DOMObject seems to be empty
	echo $serialize1."<br>";
	//unserialize 
	$unserialize1 = unserialize($serialize1);
	//----------------------------------------------the error--------------------------------------------------------
	//And now there is the error.. $unserialize1 seems to be no DOMDocument or an empty one
	$elements2 = $unserialize1->getElementsByTagName("Element2");
	//You get the error message: Warning: Couldn't fetch DOMDocument in ....
	//print_r shows an empty object
	print_r($unserialize1);
?>

Expected result:
----------------
I thought, when you unserialize a DOMDocument object you get a DOMDocument object with all data of the original one (NodeLists etc). 

Actual result:
--------------
After unserializing a serialized DOMDocument object you get an empty DOMDocument object.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-04 15:47 UTC] derick@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

.
 [2004-10-04 16:37 UTC] matthias dot hoseit at comline dot de
Thank you for your quick answer.

Sorry but I disagree. In my opinion this is a bug.

If you serialize an object (in this case a DOMDocument object which is a built-in class since PHP5 and not an external class like the domxml extension in PHP4) you get a string containing a byte-stream representation of a value (including the properties and methods since PHP4).

But in this case, after serialising the DOMDocument object you get a byte-stream representation without any properties. It is empty: "O:11:"DOMDocument":0:{}".

According to the description of serialize() in the PHP Manual, serialize() handles all types, except the resource-type. Since the PHP function is_resource() returns false, the DOMDocument object is not a resource and therefore should be serialized correctly.
 [2004-10-04 16:42 UTC] derick@php.net
You can never serialize internal classes, only user defined classes. There is no bug here.
 [2004-10-04 16:53 UTC] matthias dot hoseit at comline dot de
Ok Thank you.

Therefore this is a Documentation Problem. I should post it in that category.

And perhaps a Feature Request ;-)

Best regards Matthias Hoseit
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 05:01:29 2024 UTC