Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从字符串变量生成函数?_Php_Html - Fatal编程技术网

Php 如何从字符串变量生成函数?

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

如何从字符串生成函数?我是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) . ();

您可以这样做:

<?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()!!