开发者

how to do $class::function($data);

开发者 https://www.devze.com 2023-01-07 16:22 出处:网络
i have several classes that have the same function, the are called at different times, depending on different variables,

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消