Version: 1.0.0
Type: Class
Category: Other
License: GNU General Public License
Description: This class will serialize any inherited class to an xml doc to be deserialized for a later use
<?php /*********************************************************** Serializeable class Written By: Brad LaFountain ([email protected]) Simply extend this class from your base class and any inherited classes will be able to be serialized for use to write to file, to db, to cookie Assumptions: The class constructor for the inherited class must take no parameters or must have default values for all of the parameters. Examples: class item extends serializeable { var $_quanity; var $_item_ref; function item($item_ref = 0,$quanity = 0) { $this->_item_ref = $item_ref; $this->_quanity = $quanity; } function get_quanity() { return $this->_quanity; } function add_quanity($quanity) { $this->_quanity += $quanity; } } $item = new item(12345,"2"); $data = $item->serialize(); // do what ever you want with $data here // for this example i'll just reuse it $item2 = new item(); $item2->deserialize($data); $item2->add_quanity(1); echo $item2->get_quanity(); ***********************************************************/ class serializeable { function serialize() { $xmldoc = new_xmldoc("1.0"); $root = $xmldoc->add_root(get_class($this)); $this->_serialize_object($root,null,$this); return $xmldoc->dumpmem(); } function deserialize($data) { $xmldoc = xmldoc($data); $root = $xmldoc->root(); $this = $this->_deserialize_object($root); } // Deserialize Helpers function _deserialize_var($xmlobj) { $type = $xmlobj->getattr("type"); switch($type) { case "simple": return $this->_deserialize_simple($xmlobj); break; case "array": return $this->_deserialize_array($xmlobj); break; case "object": return $this->_deserialize_object($xmlobj); break; } } function _deserialize_array($xmlobj) { $children = $xmlobj->children(); foreach($children as $child) { $key = $child->getattr("key"); $obj[$key] = $this->_deserialize_var($child); } return $obj; } function _deserialize_simple($xmlobj) { return $xmlobj->content; } function _deserialize_object($xmlobj) { if(class_exists($xmlobj->name)) { $obj = new $xmlobj->name; $children = $xmlobj->children(); foreach($children as $child) { $name = $child->name; $obj->$name = $this->_deserialize_var($child); } } else echo $xmlobj->name . " is not a valid class"; return $obj; } // Serialize Helpers function _serialize_object($xmlobj,$name,$var) { $members = get_object_vars($var); if(is_array($members)) { if($name != null) { $child = $xmlobj->new_child($name,""); $child->setattr("type","object"); } else $child = $xmlobj; $keys = array_keys($members); foreach($keys as $key) { $this->_serialize_var($child,$key,$members[$key]); } } } function _serialize_array($xmlobj,$name,$var) { $keys = array_keys($var); $child = $xmlobj->new_child($name,""); $child->setattr("type","array"); foreach($keys as $key) { $this->_serialize_var($child,get_class($var[$key]),$var[$key]); $children = $child->children(); $children[sizeof($children)-1]->setattr("key",$key); } } function _serialize_other($xmlobj,$name,$var) { $child = $xmlobj->new_child($name,$var); $child->setattr("type","simple"); } function _serialize_var($xmlobj,$name,$var) { if(is_array($var)) { $this->_serialize_array($xmlobj,$name,$var); } elseif(is_object($var)) { $this->_serialize_object($xmlobj,$name,$var); } else { $this->_serialize_other($xmlobj,$name,$var); } } } ?>