The top-level object contains other instances of the object - the cast to (array) method affects only the top-level element. You may have to go down recursively - but I would do it differently here, given that the classes are serializable:
$transfer = serialize($myobject);
What are you going to do with other JSONified data?
If you are going to pass an object without class information, you can try using Reflection:
abstract class Object { protected function cloneInstance($obj) { if (is_object($obj)) { $srfl = new ReflectionObject($obj); $drfl = new ReflectionObject($this); $sprops = $srfl->getProperties(); foreach ($sprops as $sprop) { $sprop->setAccessible(true); $name = $sprop->getName(); if ($drfl->hasProperty($name)) { $value = $sprop->getValue($obj); $propDest = $drfl->getProperty($name); $propDest->setAccessible(true); $propDest->setValue($this,$value); } } } else Log::error('Request to clone instance %s failed - parameter is not an object', array(get_class($this))); return $this; } public function stdClass() { $trg = (object)array(); $srfl = new ReflectionObject($this); $sprops = $srfl->getProperties(); foreach ($sprops as $sprop) { if (!$sprop->isStatic()) { $sprop->setAccessible(true); $name = $sprop->getName(); $value = $sprop->getValue($this); $trg->$name = $value; } } return $trg; } }
This is the base class of most of my portable classes. It creates a stdClass object from a class or initializes a class from a stdClass object. You can easily take this for your needs (e.g. create an array).
ErnestV
source share