Re: array to object conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux