Problem with dynamic variable used as classname (static)

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

Problem with dynamic variable used as classname (static)

Post by Flamie »

Hey everyone,
Its been a long time since I posted here!
Anyhow, I'm having a little problem doing this:

Code: Select all

 
$obj_instance = {$str_class}::getInstance(Request::get('id'));
 
the error it thrown is:
Parse error: syntax error, unexpected '{' in /index.php on line 29

I know for a fact that"

Code: Select all

 
$obj_instance = new {$str_class}();
 
works with no problem, why doesnt the same concept work in a static call?
How can I work around that (please dont tell me to use eval!)

Cheers,
Marc
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Re: Problem with dynamic variable used as classname (static)

Post by Flamie »

Using eval() works but its so lame.
A big purpose of { } is to be able to use variables in such context, its frustrating that it doesn't work right.

Let me know if anyone has a real solution to this.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Re: Problem with dynamic variable used as classname (static)

Post by Flamie »

Woohoo!
I used call_user_func and it works, just make sure that hte first parameter you send is an array for static call:

Best solution:

Code: Select all

 
$obj_instance = call_user_func(array($str_class, 'getInstance'), Request::get('id'));
 
Post Reply