我有两个函数,它们附加到钩子上,并在程序生命周期中的定义点或事件中执行。当我们想要将一些返回值从一个函数调用传递给另一个函数调用但没有重复的函数调用或污染全局范围时,通过函数和设计的最佳方法是什么。
这是,我希望有充分记录的部分代码:
abstract class Test{
abstract void t();
public void t1(){
System.out.println("Test");
}
}
答案 0 :(得分:0)
通过引用将参数传递给函数保存了我的一天。这是完整的代码:
/**
* @array hookparams Hookparams array from both of function calls
*/
$hookparams = []; // initalized empty values
function hook_FunctionOne(&$hookparams)
{
//do something, than save result in hookparams array
return $hookparams['key'] = "Foo"; // return? But I can't call FunctionOne once more, and assign return value to temp variable
}
function hook_FunctionTwo(&$hookparams)
{
//do something with returned hookparams array form function, than save result in hookparams array again
return $hookparams['key'] .= "Bar"; // Result is "FooBar";
}
// Both functions are called here
echo hook_FunctionOne($hookparams); // First function call
echo hook_FunctionTwo($hookparams); // Second function call
echo "<pre>";
print_r($hookparams);
echo "</pre>"