php 什么是回调函数以及如何在 OOP 中使用它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/231327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:00:11  来源:igfitidea点击:

What is a callback function and how do I use it with OOP

phpoopdomcallbackfunction-calls

提问by Paul M

I want to use the php simple HTML DOM parserto grab the image, title, date, and description from each article on a page full of articles. When looking at the API I notice it has a set_callback which Sets a callback function. However im not sure what this does or how I would use it? In one of the examples its used to call a function which strips out some stuff, im wondering if you have to use this to call all functions?

我想使用php 简单的 HTML DOM 解析器从充满文章的页面上的每篇文章中获取图像、标题、日期和描述。在查看 API 时,我注意到它有一个 set_callback,它设置了一个回调函数。但是我不确定这是做什么或我将如何使用它?在其中一个示例中,它用于调用一个删除一些内容的函数,我想知道您是否必须使用它来调用所有函数?

I guess im wondering why I use this, and what does it do as I have never come across a callback function before!

我想我想知道我为什么使用它,它有什么作用,因为我以前从未遇到过回调函数!

回答by Mark Biek

Here's a basic callback function example:

这是一个基本的回调函数示例:

<?php

function thisFuncTakesACallback($callbackFunc)
{
    echo "I'm going to call $callbackFunc!<br />";
    $callbackFunc();
}

function thisFuncGetsCalled()
{
    echo "I'm a callback function!<br />";
}

thisFuncTakesACallback( 'thisFuncGetsCalled' );
?>

You can call a function that has its name stored in a variable like this: $variable().

您可以调用名称存储在变量中的函数,如下所示: $variable()

So, in the above example, we pass the name of the thisFuncGetsCalledfunction to thisFuncTakesACallback()which then calls the function passed in.

因此,在上面的示例中,我们将thisFuncGetsCalled函数的名称传递给thisFuncTakesACallback(),然后调用传入的函数。

回答by Owen

A callback function will use that function on whatever data is returned by a particular method.

回调函数将对特定方法返回的任何数据使用该函数。

I'm not sure how this particular library works, but it could be something as simple as:

我不确定这个特定的库是如何工作的,但它可能很简单:

$html = file_get_html('http://example.com');
$html->set_callback('make_bold');
$html->find('#title'); // returns an array

function make_bold($results) {
// make the first result bold
  return '<b>'.$results[0].'</b>';
}

ie, The function "make_bold()" will be run on any data found. Again, I'm not sure how this particular library works (ie, what methods the callback function will get called on)

即,函数“ make_bold()”将在找到的任何数据上运行。同样,我不确定这个特定的库是如何工作的(即回调函数将被调用的方法)

回答by troelskn

A callback is either a function, an object instance' method, or a static method on a class. Either way, it's kind of a function pointer. In some languages, functions are a specific type. So you could assign a function to a variable. These are generally called function oriented languages. A good example is Javascript.

回调可以是函数、对象实例的方法或类上的静态方法。无论哪种方式,它都是一种函数指针。在某些语言中,函数是一种特定类型。因此,您可以将函数分配给变量。这些通常称为面向函数的语言。一个很好的例子是 Javascript。

In PHP, a callback can be any of:

在 PHP 中,回调可以是以下任何一种:

$fn = 'foo'; // => foo()
$fn = array($obj, 'foo'); // => $obj->foo()
$fn = array('Foo', 'bar'); // => Foo::bar()

See the manual entry for is_callable.

参见手册条目is_callable

You can invoke a callback with the rather verbose function call_user_func.

您可以使用相当冗长的函数调用回调call_user_func

回答by troelskn

Defination

定义

A callbacks/callable is a simple function(either it is anonymous or named function) that we pass to another function as function parameter which in the result returns that passed function.

回调/可调用是一个简单的函数(无论是匿名函数还是命名函数),我们将其作为函数参数传递给另一个函数,该函数在结果中返回传递的函数。

Example

例子

function iWillReturnCallback($callBackHere){
    return $callBackHere;
}

function iAmCallBack(){
    echo "I am returned with the help of another function";
}

iWillReturnCallback(iAmCallBack());

//--Output -> I am returned with the help of another function

Don't be confused

不要混淆

There are some default functions in php that accepts the name of the callback function as a string in their parameter because of avoiding conflicting between the constant name and function name. So don't be confused in these kind of things.

php 中有一些默认函数,为了避免常量名和函数名之间的冲突,在它们的参数中接受回调函数的名称作为字符串。所以不要对这些事情感到困惑。

回答by Deepak Bawa

With PHP 5.3, you can now do this:

使用PHP 5.3,您现在可以执行以下操作:

function doIt($callback) { $callback(); }

doIt(function() {
    // this will be done
});

Finally, a nice way to do it. A great addition to PHP, because callbacks are awesome.

最后,一个很好的方法来做到这一点。一个很好的补充PHP,因为回调很棒。