[UPHPU] subsituting variable into new object command

Scott Mattocks scott at crisscott.com
Fri Jun 30 14:35:57 MDT 2006


jtaber wrote:
> I'm writing a test module (which I'll show at next hacker mtg (thurs
> 6th) but I need to subsitute object names into a command to instantiate
> new objects ie:
> 
> public function test1($objname='test') {
>   $obj = new $objname;  // this doesn't work
>   compare($obj->size, ......)
> }

This works for me from the commandline:
php -r '$objname = "stdClass"; $obj = new $objname(); var_dump($obj);'

Alternatively, you could use reflection:
$r = new ReflectionClass($objname);
$obj = $r->newInstance():

-- 
Scott Mattocks
Author of: Pro PHP-GTK
http://www.crisscott.com


More information about the UPHPU mailing list