Page 1 of 1

Problem with dynamic variable used as classname (static)

Posted: Fri Feb 06, 2009 4:11 am
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

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

Posted: Fri Feb 06, 2009 4:29 am
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.

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

Posted: Fri Feb 06, 2009 4:47 am
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'));