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

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

问题描述

我一直在想是否可以在 PHP 中将函数作为参数传递;我想要你在 JS 中编程时的样子:

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
});

我想要的是在exampleMethod中的某处执行该函数.这在 PHP 中可能吗?

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

推荐答案

如果您使用的是 PHP 5.3.0 或更高版本,则有可能.

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

请参阅手册中的匿名函数.

在您的情况下,您可以像这样定义 exampleMethod:

In your case, you would define exampleMethod like this:

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

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

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