这个评估代码有什么错误?
eval('class this { ');
eval('function this($l1,$l2) {
if($l1==0) { throw new ErrorException("error"); }
echo $l1.$l2;
}}');
try {
$t = new this(0,1);
}
catch(Exception $e) {
echo $e->getMessage();
}
为什么这段代码不起作用?
错误:
Parse error: syntax error, unexpected $end, expecting T_FUNCTION in .......(4) : eval()'d code on line 1
Parse error: syntax error, unexpected '}' in.......(9) : eval()'d code on line 4
Fatal error: Class 'this' not found in .......on line 12
eval('class this { ');
eval('function this($l1,$l2) {
if($l1==0) { throw new ErrorException("error"); }
echo $l1.$l2;
}}');
try {
$t = new this(0,1);
}
catch(Exception $e) {
echo $e->getMessage();
}
Why is this code not working?
Errors:
Parse error: syntax error, unexpected $end, expecting T_FUNCTION in .......(4) : eval()'d code on line 1
Parse error: syntax error, unexpected '}' in.......(9) : eval()'d code on line 4
Fatal error: Class 'this' not found in .......on line 12
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能
eval()
像class this {
这样的未封闭语句。您必须将整个块放入一个字符串中并运行eval()
一次。也就是说,首先可能没有理由使用
eval()
。你想做什么?You can't
eval()
an unclosed statement likeclass this {
. You'd have to put the entire block into one string and run theeval()
once.That said, there is probably no reason to use
eval()
in the first place. What are you trying to do?eval
接受完整且有效 的 php 代码。class this {
无效,因为最后没有}
。eval
accepts complete and valid piece of php code.class this {
is not valid, bacause there is no}
in the end.就像 Pekka 所说,尽管您可以将代码附加到变量中的
eval()
中,如下所示:Like Pekka said, though you could append the code to
eval()
in a variable like so: