Creating Object Names on the Fly

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
Jim1
Forum Newbie
Posts: 2
Joined: Fri May 28, 2010 2:50 pm

Creating Object Names on the Fly

Post by Jim1 »

Hi all,

I'm having some problems creating an object name on the fly. I'd assume it was the following to generate 'new TestObject()':

Code: Select all

$objectname = "Test";

$object = new {$objectname}Object()
But that doesn't work. What's the correct syntax to do this?

Thanks for any replies,

Jim.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Creating Object Names on the Fly

Post by Eran »

The entire name needs to be a string.

Code: Select all

$objectname = "TestObject";
$object = new $objectname;
Jim1
Forum Newbie
Posts: 2
Joined: Fri May 28, 2010 2:50 pm

Re: Creating Object Names on the Fly

Post by Jim1 »

Thanks, that worked great!
Post Reply