note 77796 deleted from function.serialize by aharvey
| From: | aharvey@php.net | Date: | Tue, 06 Nov 2012 02:12:10 +0000 |
| Subject: | note 77796 deleted from function.serialize by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191779@lists.php.net to get a copy of this message | ||
Note Submitter: aksel at metal dot ee
----
To serialize objects with fields that contain unicode values use that logic.
Note! Obviously it does not convert correctly possible unicode values in arrays that may be member
values of the object. Use an array class for that that inherits from the same base object that
implements that serialization;)
<?php
/**
* Serializes object with/without unicode fields
* Before serializing encodes unicode values
*
* @return string
*/
public function serialize(){
$serialized = array();
foreach(array_keys(get_class_vars(get_class($this))) as $key){
if(is_array($this->$key) ){
eval('$serialized["'.$key.'"] =
serialize($this->'.$key.');');
}else{
eval('$serialized["'.$key.'"] =
utf8_encode($this->'.$key.');');
}
}
$str = serialize($serialized);
return $str;
}
/**
* Unserializes object with/without unicode fields
*
* @param string Serialized data
*/
public function unserialize($serialized){
$data = unserialize($serialized);
foreach($data as $prop => $val){
if(is_array($this->$prop) ){
$this->$prop = unserialize($val);
}else{
$this->$prop = utf8_decode($val);
}
}
}
?>