如何通过多个级别 eval() PHP 代码?
我有这样的代码:
$layout_template = template_get("Layout");
$output_template = template_get("Homepage");
$box = box("Test","Test","Test");
eval("\$output = \"$layout_template\";");
echo $output;
在 $template_layout 变量中是对 变量 $output_template,因此脚本移至 $output_template 变量
但它不会再进一步,$output_template 内部是对变量 $box 的调用,但它不会进一步超过一级
I have this code:
$layout_template = template_get("Layout");
$output_template = template_get("Homepage");
$box = box("Test","Test","Test");
eval("\$output = \"$layout_template\";");
echo $output;
In the $template_layout variable is a call for the
variable $output_template, so then the script moves onto the $output_template variable
But it doesn't go any further, inside the $output_template is a call to the variable $box, but it doesn't go any further than one level
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我永远不会想要嵌套的
eval()
,尤其是在任何递归逻辑中。坏消息。请改用 PHP 的 Include。 IIRCeval()
创建一个新的执行上下文,但会产生开销,而include()
则不会。如果您有这样的缓冲区:
我有时有像上面这样的
Index.tpl
之类的文件,可以访问这样的关联数组,那么您只需在您的类中执行以下操作:签出 ob_start() 和其他输出缓冲区控件
如果您确实使用
eval()
或任何类型的用户数据包含,在清理不良代码的输入时要非常安全。看起来您正在编写某种组合的小部件/模板系统。将您的小部件(视图)编写为类,并允许它们在现有模板系统中使用。使用
$myWidget->render($model)
等保持通用。我在 PHP doc-user-comments-thingy 上看到了这一点,这似乎是一个坏主意:
也许有人可以启发我:P
I would never want nested
eval()
, and especially not in any recursive logic. Bad news. Use PHP's Include instead. IIRCeval()
creates a new execution context, with overhead whereasinclude()
doesn't.If you have buffers such as:
I sometimes have files like
Index.tpl
such as above that access an associative array like this, then you just do in your class:Checkout ob_start() and other output buffer controls
If you do use
eval()
or any kind of user data inclusion, be super safe about sanitizing inputs for bad code.It looks like you are writing a combined widget/template system of some kind. Write your widgets (views) as classes and allow them to be used in existing template systems. Keep things generic with
$myWidget->render($model)
and so on.I saw this on the PHP doc-user-comments-thingy and it seems like a bad idea:
Perhaps someone can enlighten me on that one :P