Object to array conversion oddity

From: Date: Sat, 23 Sep 2006 14:40:25 +0000
Subject: Object to array conversion oddity
Groups: php.general 
Request: Send a blank email to php-general+get-242146@lists.php.net to get a copy of this message
A simple class like this: class Myclass() { public $field1 = ''; private $field2 = ''; public function __sleep() { return array('field1', 'field2'); } } $myclass = new Myclass; If I var_dump this, I get: object(Myclass)#6 (2) { ["field1"]=> string(0) "" ["field2:private"]=> string(0) "" } If I coerce this to an array: $arr = (array)$myclass; the properties change names in a most unhelpful way: array(2) { ["field1"]=> string(0) "" ["Myclassfield2"]=> string(0) "" } The docs (http://www.php.net/manual/en/language.types.array.php) say: "If you convert an object to an array, you get the properties (member variables) of that object as the array's elements. The keys are the member variable names." It seems that's not quite true. How can I stop it doing this? Looks a bit buggy to me. Marcus -- Marcus Bointon Synchromedia Limited: Creators of http://www.smartmessages.net/ marcus@synchromedia.co.uk | http://www.synchromedia.co.uk/

« previous php.general (#242146) next »