PHP如何根据已知的变量/函数/类的字符串名称,来获取变量的值、执行该函数、新建类实例等。
如下一一道破:
根据变量名(String)获取变量值
下面列举三种形式:
- ${局部变量名}
- self::${静态变量名}。如self::${$router_name}
- $this->{类变量名}
根据函数名(String)获取变量值
下面是函数的形式:
根据类名(String)创建类对象
$class_name = "Test";
$test = new $class_name;
$test->run();
Of course, the pre-condition is the class Test is exist, and contain a public method name run;
本文介绍PHP中如何通过字符串动态地获取变量值、调用函数及创建类实例的方法。包括使用${变量名}
269

被折叠的 条评论
为什么被折叠?



