php - Cast object to stdClass -
i have database model __get , __set functions prevent dynamic creation of properties (to prevent typing mistakes):
class usermodel { public $name; public $languagecode; // f.e. "en", "nl", ... public function __get($name) { throw new exception("$name not member"); } public function __set($name, $value) { throw new exception("$name not member"); } } now, have array of usermodel instances ($users) want pass templating engine. therefore, want run array_map on array , add additional property languagetext, used in template.
$users = array_map(function ($v) { $v = (object)$v; // doesn't cast away usermodel type $v->languagetext = getlanguagetext($v->languagecode); return $v; }, $users); // pass $users templating engine of course line $v->languagetext = ... throws error because try add dynamic property. tried this: $v = (object)$v; cast usermodel object stdclass, type unchanged. ideas how cast away usermodel without having serialize/unserialize data?
i'm using php 5.3.5
you have double cast want...
$v = (object)(array)$v;
Comments
Post a Comment