变量引用作为函数参数 [英] Variable Reference as Function Argument
问题描述
有关具有动作帮助程序的Zend应用程序的全部信息.
All about a Zend Application with an action helper.
我想通过函数取消设置一些数组对.
helper:
class Application_Controller_Action_Helper_TestHelper extends Zend_Contr[...]
{
public function direct(&$array)
{
if(isset($array['key']))
unset($array['key']);
}
}
controller:
$this->_helper->TestHelper($var);
如何使它正常工作?
推荐答案
您还必须将其作为参考传递:
$this->_helper->TestHelper(&$var);
You must also pass it as reference:
$this->_helper->TestHelper(&$var);
更新: Ups,我关闭了我的错误.您(现在是我)正在收到错误因为...
UPDATE: Ups, I had my errors turned off. You (and now me) are getting the error because...
在函数调用上没有参考符号-仅在函数上 定义.仅函数定义就足以正确传递 通过引用的论点.从PHP 5.3.0开始,您将收到警告 说您使用&时不赞成使用通过引用传递时间" 在foo(& $ a);中.
There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);.
ZF的HelperBroker使用return call_user_func_array(array($helper, 'direct'), $args);
调用您的direct()方法.检查文档,但看来call_user_func_array
通过了引用,尽管有一些怪癖.
ZF's HelperBroker uses return call_user_func_array(array($helper, 'direct'), $args);
to call your direct() method. Check the docs, but it seems call_user_func_array
passes by reference, although with several quirks.
查看此答案.
这篇关于变量引用作为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!