如何从字符串中创建函数?我是php编程的新手,想要下面代码的解决方案。
如果我想访问test2.php的页面?page = test下面的类:
class test{
public static function getTest(){
//code here....
};
}
并想使用变量访问test2.php?page = test,那我该怎么办?
require_once "test.php";
$variabe = $_GET['page'];
test::get . ucfirst($variable) . ();
答案 0 :(得分:1)
你可以这样做:
<?php
class test{
public static function getTest(){
echo 'getTest()!!';
}
}
$variable = 'Test';
// since we are interpolating a raw string, input from $_GET, and a function we need to let PHP know to fully complete this string before using it as a function call
test::{"get".ucfirst($variable)}();
// Build the full method name ahead of time and you can just call it using the variable
$variable2 = 'getTest';
test::$variable2();
输出:
getTest()!!