Serializing a plain PHP class to an associative array -
consider simple class:
class token{ private $hash = ""; private $userid = ""; public function __construct($hash, $userid) { $this->hash = $hash; $this->userid = $userid; } public function gethash() { return $this->hash; } public function getuserid() { return $this->userid; } public function sethash($hash) { $this->hash = $hash; } public function setuserid($userid) { $this->userid = $userid; } }
trying serialize associative array, so:
// token $t = new token("samplehash", "sampleuser"); // array comparison $retobj = array(); $retobj['hash'] = $t->gethash(); $retobj['userid'] = $t->getuserid(); print_r((array) $t); echo "<br>"; print_r($retobj);
i this:
array ( [�token�hash] => samplehash [�token�userid] => sampleuser ) array ( [hash] => samplehash [userid] => sampleuser )
what's going on? how fix serialization second print line?
from http://www.php.net/manual/en/language.types.array.php#language.types.array.casting
if object converted array, result array elements object's properties. keys member variable names, few notable exceptions: integer properties unaccessible; private variables have class name prepended variable name; protected variables have '*' prepended variable name. these prepended values have null bytes on either side....
answer: must public
Comments
Post a Comment