You are here: Re: [PHP] array to object conversion « PHP « IT news, forums, messages
Re: [PHP] array to object conversion

Posted by Jochem Maas on 12/15/05 13:03

Cyril wrote:
> Is there any way of converting an array to an object of a type/class
> other than stdClass?

write a function? - bare in mind this is just an idea -
it would need more work to make it usuable (and the code
is completely untested):

class Test {}
function castFromArray($class, $arr)
{
$x = new $class;
foreach ($arr as $k => $v) {$x->$k = $v;}
return $x;
}

$t = castFromArray('Test', array("test1" => 123,
"test2" => 345,
"test3" => "abc"));

var_dump($t);

> I have been using the new PDO system and have experimented with
> fetching rows with PDO::FETCH_CLASS
> I would quite like to do the same with a plain array.
> Any comments appreciated.
>
> Cyril
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация