将字符串评估为PHP代码? [英] Evaluate a string as PHP code?
问题描述
我有一个想让PHP阅读的字符串作为一段代码.原因是我想提前为PHP构建指令集,然后在以后执行它.目前,我有:
I have a string that I want to have PHP read as a piece of code. The reason is that I want to build the set of instructions for PHP in advance, and then execute it later on. Currently I have:
$string = '$this->model_db->get_results()';
期望的结果是:
$string2 = $this->model_db->get_results();
推荐答案
您可以具有可变的变量/函数,但不能具有可变的方法链.但是,您可以使用变量/函数来创建方法链.
you can have a variable variable/function, but cannot have variable method chains. you can however create a method chain using variable variables/functions.
检查php文档的以下页面: http://php.net/manual/en/language.variables.variable.php
Check this page of the php documentation: http://php.net/manual/en/language.variables.variable.php
它显示了使用字符串作为对象或方法名称的用法.使用eval可能会导致安全漏洞,具体取决于数据的来源.
it shows the usage of using strings as object or method names. using eval may lead to security vulnerabilities depending on the source of your data.
$var1 = 'model_db';
$var2 = 'get_results';
$this->$var1->$var2();
这篇关于将字符串评估为PHP代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!