tags:

views:

44

answers:

1

hi there,

i have several classes that have the same function, the are called at different times, depending on different variables,

so what i need is something like

$class = 'test';
$return = $class::do_something();

but i get Parse error: parse error, unexpected t_paamayim_nekudotayim.

which from the looks of it means unexpected ::.


Update:
Also, why does doing it directly this way work on my localhost, but not on my prod server? is there a php_ini setting?

+4  A: 

You need to use a special function called call_user_func in order to dynamically call class functions.

Like so:

call_user_func($class . '::do_something');

For functions with parameters, you'll want to use call_user_func_array:

call_user_func_array($class . '::do_something', array($data));

To answer your second question, the ability to call static methods on variable classnames was only added in PHP 5.3.0, this is why your code works on one server while it throws an error on another.

Jacob Relkin
well the class looks like this class test { public static function do_something($something) { }}so what one would i be using? cheers
Hailwood
@Hailwood, i edited my answer.
Jacob Relkin
Cheers, im going to mark this as the accepted answer asap, but are you also able to answer my question in the update?
Hailwood