Instantiate by var name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Instantiate by var name

Post by Todd_Z »

If I have a string, for example $str = "Class"; How can I cause $obj to be the result of new Class()?
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

I love how I sit here thinking about how to solve a problem for 10 minutes, then immediately after posting, the answers pops into my Head. This is another one of those moments where you just have to say "I love you, php"

Code: Select all

$class = "ClassName";
$obj = new $class( $arg[0], $arg[1], ... );
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Glad we could help.
Post Reply