PHP:回调函数 [英] PHP: Callback functions

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

问题描述

PHP中的一些函数需要一个回调函数。我怎么能在一个函数自己做这个?首先,我如何定义一个需要回调函数的函数?

Some functions in PHP require a callback function. How can I do this in a function myself? First of all, how do I define a function that require a callback function?

其次,如何提供一个自定义函数作为回调函数?如何提供常规函数,实例函数和静态函数?

And secondly, how do I provide a custom function as the callback function? How do I provide a regular function, an instance function and a static function?

推荐答案

使用内置的 call_user_func()。可能需要使用 call_user_func_array()

Use the built in call_user_func(). It may be necessary to use call_user_func_array()

function work($a, $c) {
  $a = filter($a)
  if(!is_callable($c) || !call_user_func($c, $a)) {
    return 0; // throw error
  } else {
    return 1; // return success
  }
}

code> $ c($ a) aka passed_callback(passed_argument)因为检查函数实际上是否存在是为你做的,虽然有些人评论了性能下降 $ c($ a)

This is safer than just doing $c($a) a.k.a passed_callback(passed_argument) because checking to see if the function actually exists is done for you, though some have commented on performance degradation over $c($a).

这篇关于PHP:回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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