Php 如何从字符串变量生成函数?
如何从字符串生成函数?我是php编程新手,需要以下代码的解决方案 如果我想访问test2.php的页面?page=test用于以下类:Php 如何从字符串变量生成函数?,php,html,Php,Html,如何从字符串生成函数?我是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 . u
class test{
public static function getTest(){
//code here....
};
}
想要使用变量访问test2.php?page=test,那么我应该怎么做
require_once "test.php";
$variabe = $_GET['page'];
test::get . ucfirst($variable) . ();
您可以这样做:
<?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();
欢迎使用MANOJ03H,请点击右上/下投票箭头<代码>:- /代码>,请将此作为正确答案。
getTest()!!