how to do $class::function($data);
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开发者_JS百科 way work on my localhost, but not on my prod server? is there a php_ini setting?
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.
精彩评论