Creating an object based on a variable?

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
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Creating an object based on a variable?

Post by Flamie »

Say I have like:

Code: Select all

$npc_name = "Mayor";
What I want to do is something like this:

Code: Select all

$npc = new $npc_name(); //I wnat this to act as $npc = new Mayor();
So is it doable in someway?
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

PHP Manual: variable variables
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

thanks a lot.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Creating an object based on a variable?

Post by Christopher »

Flamie wrote:

Code: Select all

$npc_name = "Mayor";
$npc = new $npc_name(); //I wnat this to act as $npc = new Mayor();
So is it doable in someway?
Did you try it before you asked the question?
(#10850)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Creating an object based on a variable?

Post by Oren »

arborint wrote:Did you try it before you asked the question?
:P I don't get this kind of posts at all... Couldn't you just try it?
Post Reply