什么是回调函数,如何在OOP中使用它 [英] What is a callback function and how do I use it with OOP
问题描述
我想使用 php simple HTML DOM解析器来获取图像,标题,日期,以及每篇文章中关于文章的描述。当看到API时,我注意到它有一个set_callback设置一个回调函数。但是我不知道这是做什么或我会如何使用它?在其中一个例子中,它用来调用一些功能,删除一些东西,我想知道你是否必须使用它来调用所有功能?
I want to use the php simple HTML DOM parser to 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?
我想我想知道为什么我使用这个,它做什么,因为我从来没有遇到过回调函数!
I guess im wondering why I use this, and what does it do as I have never come across a callback function before!
推荐答案
这是一个基本的回调函数示例:
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' );
?>
您可以调用名称存储在这样一个变量中的函数: $ variable ()
You can call a function that has its name stored in a variable like this: $variable().
所以,在上面的例子中,我们将 thisFuncGetsCalled 函数的名称传递给 thisFuncTakesACallback ),然后调用传入的函数。
So, in the above example, we pass the name of the thisFuncGetsCalled function to thisFuncTakesACallback() which then calls the function passed in.
这篇关于什么是回调函数,如何在OOP中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!