PHP eval 返回 null 但调用了 return
我有一个我认为相当简单的问题,但我不知道我做错了什么。我有一个用 PHP 函数 eval
调用的函数。我期待得到一个自建的 ArrayList 结果。但当我使用 gettype
时,我看到结果是 NULL
。即使我在 eval 函数中使用了 return 。
据我所知,我正在按照文档播放它,但不知何故它不起作用。有什么建议吗?
代码片段
<?php
$widgetList = new ArrayList();
for($i = 0; $i < $selectedTemplate->Regions->count(); $i++)
{
$region = $selectedTemplate->Regions->item($i);
if($region->Widget->selectiveContent == 1)
{
$widgetList->add($region->Widget);
}
}
if($widgetList->count() > 0)
{
?>
<tr>
<td colspan="2">
<strong>Widget instellingen</strong>
<hr size="1" width="100%" color="#333"/>
</td>
</tr>
<?php
for($i = 0; $i < $widgetList->count(); $i++)
{
?>
<tr>
<td class="w150">
<?= $widgetList->item($i)->title ?>
</td>
<td>
<select name="widget_<?= $widgetList->item($i)->id ?>" class="full">
<?php
$itemList = eval($widgetList->item($i)->functionCall);
for($j = 0; $j < $itemList->count(); $j++)
{
$selected = null;
if($_POST["widget_".$widgetList->item($i)->id] == $itemList->item($j)->id)
{
$selected = " selected=\"selected\"";
}
?>
<option value="<?= $itemList->item($j)->id ?>"<?= $selected ?>><?= $itemList->item($j)->title ?></option>
<?php
}
?>
</select>
</td>
</tr>
<?php
}
}
?>
评估代码
public function getNavigationByLanguageId(Integer $parent, ArrayList $objectList, Integer $language)
{
$query = DataAccess::getAdapter()->query("
SELECT *
FROM `navigation`
WHERE `parent_id` = ".$parent->value."
AND `language_id` = ".$language->value."
AND `website_id` = ". $_SESSION["currentSite"]["id"]."
ORDER BY `sort_order`");
while($result = DataAccess::getAdapter()->fetchAssoc($query))
{
$link = new Model_Navigation();
$link->id = $result["id"];
$link->language = $result["language_id"];
$link->parent = $result["parent_id"];
$link->Page = Model_Page::getPageById(new Integer($result["page_id"]));
$link->title = $result["title"];
$link->externalUrl = $result["external_url"];
$link->sortOrder = $result["sort_order"];
$objectList->add($link);
Model_Navigation::getNavigationByLanguageId(new Integer($result["id"]), $objectList, $language);
}
return $objectList;
}
正在评估的函数是: Model_Navigation::getNavigationByLanguageId(new Integer(0), new ArrayList(), new Integer( 7));
关于代码的更多解释 我正在我的新 CMS 中使用此功能。用户能够设计模板并将小部件放置在定义的区域中,非常类似于 jQuery 提供的 portlet 功能。我为网站安装了小部件,我内置了一个选项来提供函数调用,以在小部件被标记为“选择性内容”时检索对象列表。
我在这里想要实现的是使用 eval
从 $widget->functionCall 的值返回 ArrayLists。
I have a, I think fairly easy, question, but I can't figure out what I'm doing wrong. I have a function which i call with PHP's function eval
. I'm expecting an, selfbuilt, ArrayList to get as a result. But instead when I use gettype
I see the result is NULL
. Even though I'm using return
in the eval-ed function.
As far as I'm aware I'm playing it by the documentation, but somehow it's not working. Any suggestions?
Code fragment
<?php
$widgetList = new ArrayList();
for($i = 0; $i < $selectedTemplate->Regions->count(); $i++)
{
$region = $selectedTemplate->Regions->item($i);
if($region->Widget->selectiveContent == 1)
{
$widgetList->add($region->Widget);
}
}
if($widgetList->count() > 0)
{
?>
<tr>
<td colspan="2">
<strong>Widget instellingen</strong>
<hr size="1" width="100%" color="#333"/>
</td>
</tr>
<?php
for($i = 0; $i < $widgetList->count(); $i++)
{
?>
<tr>
<td class="w150">
<?= $widgetList->item($i)->title ?>
</td>
<td>
<select name="widget_<?= $widgetList->item($i)->id ?>" class="full">
<?php
$itemList = eval($widgetList->item($i)->functionCall);
for($j = 0; $j < $itemList->count(); $j++)
{
$selected = null;
if($_POST["widget_".$widgetList->item($i)->id] == $itemList->item($j)->id)
{
$selected = " selected=\"selected\"";
}
?>
<option value="<?= $itemList->item($j)->id ?>"<?= $selected ?>><?= $itemList->item($j)->title ?></option>
<?php
}
?>
</select>
</td>
</tr>
<?php
}
}
?>
Eval-ed code
public function getNavigationByLanguageId(Integer $parent, ArrayList $objectList, Integer $language)
{
$query = DataAccess::getAdapter()->query("
SELECT *
FROM `navigation`
WHERE `parent_id` = ".$parent->value."
AND `language_id` = ".$language->value."
AND `website_id` = ". $_SESSION["currentSite"]["id"]."
ORDER BY `sort_order`");
while($result = DataAccess::getAdapter()->fetchAssoc($query))
{
$link = new Model_Navigation();
$link->id = $result["id"];
$link->language = $result["language_id"];
$link->parent = $result["parent_id"];
$link->Page = Model_Page::getPageById(new Integer($result["page_id"]));
$link->title = $result["title"];
$link->externalUrl = $result["external_url"];
$link->sortOrder = $result["sort_order"];
$objectList->add($link);
Model_Navigation::getNavigationByLanguageId(new Integer($result["id"]), $objectList, $language);
}
return $objectList;
}
The function being eval-ed is: Model_Navigation::getNavigationByLanguageId(new Integer(0), new ArrayList(), new Integer(7));
Some more explaination about the code
I'm using this funcitonality in my new CMS. A user is able to design a template and place widgets in defined regions, pretty much like the portlet functionality jQuery is providing. Widgets are installed for a website by me, I have built in an option to provide a functioncall to retrieve a list of objects when a widget is flagged as 'Selective Content'.
What I'm trying to achieve here is using eval
to return ArrayLists from the value of $widget->functionCall.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这一行 $widgetList->item($i)->functionCall 的内容是什么?
$itemList = eval($widgetList->item($i)->functionCall);
要让 eval 返回任何内容,$widgetList->item($i)->functionCall 必须以标记
return
开头,例如,if
then 你没问题
,但是 if
then 这就是 eval() 的原因不返回任何东西。另外,请留意执行 eval() 时生成的任何错误 - 如果您的代码无法解析或抛出任何类型的错误,这也可以解释为什么 eval() 不返回任何内容。
不过,您应该能够完全取消对 eval 的调用——最坏的情况是使用 call_user_func()
编辑:
对于您的示例:
这可以写为:
这可能是更好的选择,因为它避免了编译开销,不太可能中断等。
On this line, what is the content of $widgetList->item($i)->functionCall ?
$itemList = eval($widgetList->item($i)->functionCall);
For eval to return anything, $widgetList->item($i)->functionCall MUST begin with the token
return
e.g., if
then you're fine
But if
Then that's why eval() isn't returning anything. Also, keep an eye out for any errors generated while executing eval() -- if you code fails to parse, or throws any sort of error, that may also explain why eval() isn't returning anything.
You should be able to do away with the call to eval entirely, though -- at worst, use call_user_func()
Edit:
For your example:
This could be written as:
This is probably preferable, as it avoids compilation overhead, is less likely to break, etc.
如果运行,
输出将为“NULL”。尽管 bar() 返回值 foo() 却没有。 bar()的返回值是“丢失”。如果您使用 eval()
打印,则相同
If you run
the output will be `NULL'. Although bar() returns a value foo() does not. The return value of bar() is "lost". Same if you use eval()
prints
Eval 是邪恶的(并且在大多数情况下是 hackish 的)。我建议发布您的代码,我们也许可以为您删除这部分代码。
Eval is evil (and in most cases hackish). I would suggest posting your code and we can perhaps do away with that portion of the code for you.
如果您在 eval 函数之外运行代码并且没有问题,那么您可能需要检查 eval() 函数文档。
确保要“评估”的字符串“没有错误”/格式正确。
If you run the code outside the eval function and it's OK then you probably need to check the eval() function documentation.
Make sure the string to be "evaluated" is "not wrong"/formatted properly.