从 eval() 生成 lambda 函数
我有几个助手 - 我想将每个助手的方法重新声明为 lambda 匿名函数。
我试图通过获取辅助方法,然后执行 eval 函数来实现此目的,但它不起作用,我收到解析错误。
我当前的代码:
foreach($this->helpers as $helper)
{
include(master_path . 'helpers/'.$helper.'Helper.php');
$helperClass = new applicationHelper();
$methods = get_class_methods($helperClass);
foreach($methods as $method )
{
eval ( "\$$method = function (\$text) {
\$helperClass->$method(\$text);
}");
}
}
由于效率担忧 - 如果您知道,我想要一个更好的解决方案它,谢谢! 谢谢你们!
I have few helpers - I want to redclare each helper's method as a lambda anonymous function.
I'm trying to do it by getting the helpers methods, and then doing an eval function, but it wont work, im getting parse error..
My current code:
foreach($this->helpers as $helper)
{
include(master_path . 'helpers/'.$helper.'Helper.php');
$helperClass = new applicationHelper();
$methods = get_class_methods($helperClass);
foreach($methods as $method )
{
eval ( "\$method = function (\$text) {
\$helperClass->$method(\$text);
}");
}
}
Due to efficiency fears - I'd like a better solution if you know it, thanks!
Thanks Guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可行:
但仍然不知道你为什么这样做。
编辑
需要 PHP 5.3.x ->看这里匿名函数
That should work:
But still dont know why are you doing that.
EDIT
PHP 5.3.x needed -> look here Anonymous funcions
这样就摆脱了缓慢的
eval
。That get's rid of the slow
eval
.