在PHP中,如何将一个函数作为另一个函数中的参数?
In PHP, how do you use a function as a parameter in another function?
我知道以前可能有人问过这个问题,但我不太了解匿名函数(闭包)的整个概念,以及它们如何具体适用于我的情况。注意:我知道拥有所有这些简单的功能是很愚蠢的,但我的任务要求说我应该拥有所有这些功能:/。
我有几个功能。在下面的函数中,参数 $action1
和 $action2
都将被函数替换:
function dothis($num1, $num2, $action1, $action2)
{
$result = $num1 + $num2;
if ($result > 52){
//do $action1 which is a function
} else {
//do $action2 which is another function
}
return $result;
}
函数 dothis
将在另一个名为 add
的函数中调用。这是 add
函数:
function add($action1,$action2)
{
$answer = dothis(42, 34, $action1, $action2);
echo $answer;
}
add
函数中的$action1
和$action2
与dothis
函数中的$action1
和$action2
本质上是一样的。即使它们是相同的东西,现在是否需要使用不同的名称?
现在,add
函数将在 main
函数中调用,其中参数 $action1
和 $action2
将被替换为它们所包含的实际函数对应于:
function main()
{
add($fun1,$fun2);
echo 'Arithmetic complete';
}
这是 $fun1
和 $fun2
的代码:
$fun1 = function () {
echo 'Wow! The answer is greater than 52.';
};
$fun2 = function () {
echo 'Oh no! The answer is less than 52.';
};
我该怎么办,我需要改变什么?我真的很感激任何帮助。提前致谢!
我想这是一个相当糟糕的方法。您可以使用像 call_user_func()
but not really for anonymous functions 这样的函数。为什么不提出一个 class 来定义这两个函数,或者(如果代码相当短)而是直接在 if/else 语句中?
class 方法的一些虚拟示例代码:
class doSomething {
function dothis($num1, $num2) {
$result = $num1 + $num2;
if ($result > 52) {
$this->action1($num1, $num2);
} else {
$this->action2($num1, $num2);
}
return $result;
}
function action1($numbers) {
// do sth. here
}
function action2($num1, $num2) {
// do sth. else here
}
}
// afterwards
$pointer = new doSomething();
$pointer->dothis(21,34); // action1
$pointer->dothis(1,1); // action2
请参阅有关 Variable Functions
的 PHP 文档
你可以通过在函数后面加上 ()
来调用函数(必要时在括号内加上参数)。
function dothis($num1,$num2,$action1,$action2){
$result = $num1 + $num2;
if ($result > 52){
$action1();
}
else{
$action2();
}
return $result;
}
要使此语法与匿名函数一起使用,您必须关闭 eAccelerator。参见 Anonymous functions does not work: Function name must be a string
如果不能使用匿名函数,则需要使用命名函数。
function fun1 () {
echo 'Wow! The answer is greater than 52.';
}
$fun1 = 'fun1';
function fun2() {
echo 'Oh no! The answer is less than 52.';
}
$fun2 = 'fun2';
我知道以前可能有人问过这个问题,但我不太了解匿名函数(闭包)的整个概念,以及它们如何具体适用于我的情况。注意:我知道拥有所有这些简单的功能是很愚蠢的,但我的任务要求说我应该拥有所有这些功能:/。
我有几个功能。在下面的函数中,参数 $action1
和 $action2
都将被函数替换:
function dothis($num1, $num2, $action1, $action2)
{
$result = $num1 + $num2;
if ($result > 52){
//do $action1 which is a function
} else {
//do $action2 which is another function
}
return $result;
}
函数 dothis
将在另一个名为 add
的函数中调用。这是 add
函数:
function add($action1,$action2)
{
$answer = dothis(42, 34, $action1, $action2);
echo $answer;
}
add
函数中的$action1
和$action2
与dothis
函数中的$action1
和$action2
本质上是一样的。即使它们是相同的东西,现在是否需要使用不同的名称?
现在,add
函数将在 main
函数中调用,其中参数 $action1
和 $action2
将被替换为它们所包含的实际函数对应于:
function main()
{
add($fun1,$fun2);
echo 'Arithmetic complete';
}
这是 $fun1
和 $fun2
的代码:
$fun1 = function () {
echo 'Wow! The answer is greater than 52.';
};
$fun2 = function () {
echo 'Oh no! The answer is less than 52.';
};
我该怎么办,我需要改变什么?我真的很感激任何帮助。提前致谢!
我想这是一个相当糟糕的方法。您可以使用像 call_user_func()
but not really for anonymous functions 这样的函数。为什么不提出一个 class 来定义这两个函数,或者(如果代码相当短)而是直接在 if/else 语句中?
class 方法的一些虚拟示例代码:
class doSomething {
function dothis($num1, $num2) {
$result = $num1 + $num2;
if ($result > 52) {
$this->action1($num1, $num2);
} else {
$this->action2($num1, $num2);
}
return $result;
}
function action1($numbers) {
// do sth. here
}
function action2($num1, $num2) {
// do sth. else here
}
}
// afterwards
$pointer = new doSomething();
$pointer->dothis(21,34); // action1
$pointer->dothis(1,1); // action2
请参阅有关 Variable Functions
的 PHP 文档你可以通过在函数后面加上 ()
来调用函数(必要时在括号内加上参数)。
function dothis($num1,$num2,$action1,$action2){
$result = $num1 + $num2;
if ($result > 52){
$action1();
}
else{
$action2();
}
return $result;
}
要使此语法与匿名函数一起使用,您必须关闭 eAccelerator。参见 Anonymous functions does not work: Function name must be a string
如果不能使用匿名函数,则需要使用命名函数。
function fun1 () {
echo 'Wow! The answer is greater than 52.';
}
$fun1 = 'fun1';
function fun2() {
echo 'Oh no! The answer is less than 52.';
}
$fun2 = 'fun2';