Call static method from a string name in PHP

我只是一个虾纸丫 提交于 2019-12-05 01:25:51

This is because your server runs a version of PHP earlier than 5.3.0, in which this syntax is not supported.

From the documentation on the scope resolution operator:

As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).

In any case, you can always use call_user_func:

$class = "ModelName"; // the "new" in your example was a typo, right?
$items = call_user_func(array($class, 'model'))->findAll();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!