What is a callback function and how do I use it with OOP(什么是回调函数以及如何在 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 中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是回调函数以及如何在 OOP 中使用它


基础教程推荐
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01