在PHP中接受函数作为参数 [英] Accept function as parameter in PHP

查看:100
本文介绍了在PHP中接受函数作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想是否可以在PHP中传递一个函数作为参数;我想要的东西就像在用JS编程时一样:

  object.exampleMethod(function(){
//一些东西来执行
});

我想要在exampleMethod的某个地方执行该函数。这是可能的PHP?

解决方案

这是可能的,如果您使用PHP 5.3.0或更高版本。
$ b

请参阅手册中的匿名函数。 / p>

在你的情况下,你可以这样定义 exampleMethod

 函数exampleMethod($ anonFunc){
//执行匿名函数
$ anonFunc();
}


I've been wondering whether is possible or not to pass a function as parameter in PHP; I want something like when you're programming in JS:

object.exampleMethod(function(){
    // some stuff to execute
});

What I want is to execute that function somewhere in exampleMethod. Is that possible in PHP?

解决方案

It's possible if you are using PHP 5.3.0 or higher.

See Anonymous Functions in the manual.

In your case, you would define exampleMethod like this:

function exampleMethod($anonFunc) {
    //execute anonymous function
    $anonFunc();
}

这篇关于在PHP中接受函数作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆