ZEND 执行/调用函数存储在字符串中
我在调用名称为字符串的函数时遇到问题。
我做了一些助手,我想在我的 phtml 文件中回显如下所示:
echo $this->EditProfile();
echo $this->ViewProfile();
EditProfile() 和 ViewProfile() 是我创建的视图助手的名称,我在视图中调用它们。而且这个方法效果很好。但是,当我想通过存储在数据库中的名称动态调用函数时,我尝试以这种方式执行此操作:
我从数据库获取助手的名称并将它们存储到数组中,然后尝试在 foreach 中显示它们。
foreach ($this->modules as $key => $module)
{
echo $this->$module['name'];
}
该变量
$module['name']
包含我想在 phtml 文件中调用的 Helper 的有效名称(使用 Zend_debug::dump() 检查,并在 foeach 和 id 中仅使用 echo $module['name'] 进行检查,并且 id 正确显示它...但是这个 echo它不工作并且不调用视图助手,
当我尝试 eval 或 call_user_func 时也没有显示任何内容......我如何在 foreach 或其他循环中执行此操作?
i have a problem with calling a function which name is a string.
I made few helpers which i want to echo in my phtml file like this:
echo $this->EditProfile();
echo $this->ViewProfile();
The EditProfile() and ViewProfile() are names of the View Helpers which i created and i'm calling them in view. And this method is working fine. But when i want dynamicly call a function by name stored in database im trying to do this in this way:
im getting the names of helpers from database and store them into array and then trying to display them in foreach.
foreach ($this->modules as $key => $module)
{
echo $this->$module['name'];
}
the variable
$module['name']
contains a valid name of Helper which i want to call in phtml file (checked with Zend_debug::dump() and with just an echo $module['name'] in foeach and id display it properly... but this echo its not working and not calling the View Helper, nothing is displayed
when i try eval or call_user_func too nothing is displayed too... How can i do this in foreach or other loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我自己解决了:)
不知道这个解决方案是否正确,但它实际上有效;)
而不是 call_user_func 我提到神奇的函数 __call 与 call_user_func_array 相同,
所以我编辑了如下代码,
在这种情况下数组为空,因为没有参数传递给函数。如果我的助手需要参数,将来我会在这个数组中传递它们。
这个解决方案对我来说效果很好:)
如果有人有更好的解决方案,请在此处发布并分享您的意见;)
问候
达雷克
ok solved it myself :)
dont know is this solution properly but its actually working ;)
instead call_user_func i mentioned that magical function __call is same as call_user_func_array
so i edited code like this below
in this case array is null cause none parameters are passed to function. If in my helper ill need parameters ill pass them in this array in future.
And this solution works fine for me :)
If someone have better solution please post it here and share your opinion ;)
regards
Darek