[英]php - how to call static class and function with variable?
我已經嘗試過使用帶有和不帶有{}的代碼,但是它一直說“語法錯誤,意外的'{',期望..中的標識符(T_STRING)。
$page = $_GET['page'];
$do = $_GET['do'];
{$page}::{$do}();
然后我有一個類和該類中的一個函數,應調用該函數,如下所示。
class Example {
public static function index()
{}
}
因此,如果$ page =“ Example”和$ do =“ index”,我想調用Example :: index();。
有兩種方法可以做到這一點:
//$class: the class which contains your static function
//$method: the function you want to call
call_user_func("$class::$method");
//OR
call_user_func(array($class, $method));
在您的情況下:
call_user_func("$page::$do");
要么
call_user_func(array($page, $do));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.