Home » XML » XML_Serializer » Bug #4575
_serializeObject and __sleep
Details
| Submitted | 2005-06-11 23:58 UTC |
|---|---|
| From | patxi at eslomas dot com |
| Assigned | schst |
| Status | Closed |
| Package | XML_Serializer |
| PHP Version | 4.3.10 |
| OS | Linux |
| Roadmaps | (Not assigned) |
Comments
[2005-06-11 23:58 UTC] patxi at eslomas dot com
Description:
------------
I think there is an error in the _serializeObject method of XML_serializer. If the object to be serialized has a __sleep method, it is called, but its return value is not captured.
In fact, you get the attributes to be serialized always with get_object_vars function, however, when __sleep method exists, it must returns an array with the name of every attribute of the object to be serialized, so you shouldn't call get_object_vars in these cases.
Reproduce code:
---------------
The actual code is:
-------------------
// check for magic function
if (method_exists($object, '__sleep')) {
$object->__sleep();
}
$tmp = $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
$properties = get_object_vars($object);
>>>>>>>>>>>>
i think it should be:
---------------------
// check for magic function
if (method_exists($object, '__sleep')) {
$properties = $object->__sleep();
}
else $properties = get_object_vars($object);
$tmp = $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
[2005-09-29 18:25 UTC] chris at ilikeu2 dot nl
This bug is fixed incorrectly.
The __sleep() method returns an array of propertie _names_ and does not include the values.
Also, I think it would be wise to call the __wakeup() afterwards.
To come to the point, this is how I think the _serializeObject function should look:
function _serializeObject(&$object, $tagName = null, $attributes = array())
{
// check for magic function
if (method_exists($object, '__sleep')) {
$props = $object->__sleep();
$properties = array();
foreach($props as $prop) {
$properties[$prop] = $object->$prop;
}
} else {
$properties = get_object_vars($object);
}
if (method_exists($object, '__wakeup')) {
$object->__wakeup();
}
$tmp = $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
if (empty($tagName)) {
$tagName = get_class($object);
}
// typehints activated?
if ($this->options[XML_SERIALIZER_OPTION_TYPEHINTS] === true) {
$attributes[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]] = 'object';
$attributes[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_CLASS]] = get_class($object);
}
$string = $this->_serializeArray($properties, $tagName, $attributes);
return $string;
}