dongxiandi8313
2012-07-17 02:53
浏览 42
已采纳

从函数中调用非全局变量?

During the development of a Joomla! plugin, I ran across something really interesting. One of the events does not have a return value, but it calls variables from inside the function. Prior knowledge tells me this should only work if the variables are global inside the function, yet the dispatcher is able to call the variables from outside the function.

EDIT: I just discovered that the variable that is accessed from inside the function needs to be one of the paramaters! Could this be func_get_params() or call_user_func()?

Calling Code:

$instance = JDispatcher::getInstance();
$instance->trigger(onJoomCalledEvent, array(&$link, $other_params));

Plugin (snippet):

class plgMyPlugin extends JPlugin{

    onJoomCalledEvent($link, $other_params){
        $link = "Some Value Here";
        return false;
    }
}

This function returns false, yet somehow the application (Joomla!) is able to extract the value of $link. How is this done?

图片转代码服务由CSDN问答提供 功能建议

在开发Joomla期间! 插件,我碰到了一些非常有趣的东西。 其中一个事件没有返回值,但它从函数内部调用变量。 先前的知识告诉我,只有变量在函数内部是全局的,但调度员才能从函数外部调用变量。

编辑:我刚刚发现 从函数内部访问的变量需要是参数之一! 这可能是func_get_params()或call_user_func()吗?

调用代码:

  $ instance = JDispatcher :: getInstance  (); 
 $ instance->触发器(onJoomCalledEvent,array(& $ link,$ other_params)); 
   
 
 

插件(摘录):

 类plgMyPlugin扩展了JPlugin {
 
 on on on on on on on on on on from from from from from from from from from  n} 
   
 
 

此函数返回false,但应用程序(Joomla!)能够以某种方式提取$ link的值。 这是怎么做的?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dqssst0144 2012-07-25 00:15
    最佳回答

    Does the plug-in definition look like this:

    class plgMyPlugin extends JPlugin{
    
        onJoomCalledEvent(&$link, $other_params){
            $link = "Some Value Here";
            return false;
        }
    }
    

    Than it's pass by reference. If it's indeed the way you posted above than it's call time pass by reference which is deprecated and emits a warning starting with PHP 5.3.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题