On Mon, 2007-12-17 at 15:50 +0800, ked wrote: > Hi , I'm a freshman in PHP, can anyone give me any advices? > > I defied some simple classes, like "User", "Item"... > > in a general way , > $obj = new User(); > > specially, I need to assign a Object via a class name . > > Now , my code : > switch ($className) > { > case "User": > return new User(); > break ; > case "Item": > return new Item(); > break ; > default: > break ; > } > > I think that It's not a clever job. How to do it skillfully? > > Thank you for any advice. <?php class User { } class Item { } function getObject( $name ) { $obj = false; if( class_exists( $name ) ) { $obj = new $name(); } return $obj; } var_dump( getObject( 'User' ) ); echo"\n\n"; var_dump( getObject( 'Item' ) ); echo"\n\n"; var_dump( getObject( 'Foo' ) ); echo"\n\n"; ?> Cheers, Rob. -- ........................................................... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ........................................................... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php