我一直想知道是否可以在 PHP 中将函数作为参数传递。我想要类似于在 JavaScript 中编写以下代码时的内容:
object.exampleMethod(function(){
// some stuff to execute
});
我想要的是在 exampleMethod 中的某处执行该函数。这在 PHP 中可行吗?
最佳答案
如果您使用的是 PHP 5.3.0 或更高版本,这是可能的。
见 Anonymous Functions在手册中。
在您的情况下,您可以像这样定义 exampleMethod
:
function exampleMethod($anonFunc) {
//execute anonymous function
$anonFunc();
}
关于php - 接受函数作为 PHP 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2700433/