note 77796 added to function.serialize

From: Date: Thu, 13 Sep 2007 10:31:16 +0000
Subject: note 77796 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-130460@lists.php.net to get a copy of this message
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); } } } ?> ---- Server IP: 129.16.214.50 Probable Submitter: 194.237.142.10 ---- Manual Page -- http://www.php.net/manual/en/function.serialize.php Edit -- https://master.php.net/note/edit/77796 Del: integrated -- https://master.php.net/note/delete/77796/integrated Del: useless -- https://master.php.net/note/delete/77796/useless Del: bad code -- https://master.php.net/note/delete/77796/bad+code Del: spam -- https://master.php.net/note/delete/77796/spam Del: non-english -- https://master.php.net/note/delete/77796/non-english Del: in docs -- https://master.php.net/note/delete/77796/in+docs Del: other reasons-- https://master.php.net/note/delete/77796 Reject -- https://master.php.net/note/reject/77796 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130460) next »