|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 23:00:01 2025 UTC |
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.