什么是回调函数,我如何使用它与OOP [英] What is a callback function and how do I use it with OOP

查看:170
本文介绍了什么是回调函数,我如何使用它与OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 PHP简单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屋!

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