在 PHP 中实现回调函数

WBOY
发布: 2024-02-28 10:10:01
转载
483 人浏览过

在PHP中,回调函数是一种常见的编程技术,能够增强代码的灵活性和可复用性。通过回调函数,我们可以将函数作为参数传递给其他函数,在特定的条件下被调用。这种方式使得代码结构更加清晰,并且可以根据需要动态地改变函数的行为。本文将介绍在PHP中实现回调函数的方法,以及如何有效地利用回调函数来简化代码逻辑和提高代码的可维护性。php小编百草将为您详细讲解如何使用回调函数,让您轻松掌握这一重要的编程技术。


php 中创建一个 callback 函数并使用 call_user_func 执行

我们创建了一个名为 testFunct<code>testFunct<strong class="keylink">io</strong>n()callback 函数,并使用 call_user_func()ion()callback 函数,并使用 call_user_func() 方法通过将函数名称作为字符串

传递给该方法来执行它。

例子:

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">testFunction</span>() {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Testing Callback </span><span style="color:#b62;font-weight:bold">\n</span><span style="color:#ba2121">"</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">// Standard callback
</span></span></span><span style="display:flex;"><span>call_user_func(<span style="color:#ba2121">&#39;testFunction&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>
登录后复制
输出:
<code>
<code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Testing Callback
</span></span></code></code>
登录后复制

callback 函数并使用 array_map 在 PHP 中创建一个

方法执行

array_map 方法执行 callback 函数。这将使用传递给 array_map() 我们使用

函数的相应数据执行该方法。

例子:

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">length_callback</span>(<span style="color:#19177c">$item</span>) {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">return</span> strlen(<span style="color:#19177c">$item</span>);
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#19177c">$strings</span> <span style="color:#666">=</span> [<span style="color:#ba2121">"Kevin Amayi"</span>, <span style="color:#ba2121">"Programmer"</span>, <span style="color:#ba2121">"N<strong class="keylink">ai</strong>robi"</span>, <span style="color:#ba2121">"Data Science"</span>];
</span></span><span style="display:flex;"><span><span style="color:#19177c">$lengths</span> <span style="color:#666">=</span> array_map(<span style="color:#ba2121">"length_callback"</span>, <span style="color:#19177c">$strings</span>);
</span></span><span style="display:flex;"><span>print_r(<span style="color:#19177c">$lengths</span>);
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>
登录后复制
输出:
<code>
<code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Array ( [0] => 11 [1] => 10 [2] => 7 [3] => 12 )
</span></span></code></code>
登录后复制

在 PHP 中实现多个回调函数并使用用户定义的函数执行它们

testCallBacks() 的用户定义函数执行两个名为 nameagecallback 我们将使用名为

函数,将函数的名称作为字符串绕过用户定义的函数。

例子:

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">name</span>(<span style="color:#19177c">$str</span>) {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">return</span> <span style="color:#19177c">$str</span> <span style="color:#666">.</span> <span style="color:#ba2121">" Kevin"</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">age</span>(<span style="color:#19177c">$str</span>) {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">return</span> <span style="color:#19177c">$str</span> <span style="color:#666">.</span> <span style="color:#ba2121">" Kevin 23 "</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">testCallBacks</span>(<span style="color:#19177c">$str</span>, <span style="color:#19177c">$f<strong class="keylink">ORM</strong>at</span>) {
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">// Calling the $format callback function
</span></span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$format</span>(<span style="color:#19177c">$str</span>)<span style="color:#666">.</span><span style="color:#ba2121">"<br>"</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">// Pass "name" and "age" as callback functions to testCallBacks()
</span></span></span><span style="display:flex;"><span>testCallBacks(<span style="color:#ba2121">" Hello"</span>, <span style="color:#ba2121">"name"</span>);
</span></span><span style="display:flex;"><span>testCallBacks(<span style="color:#ba2121">" Hello"</span>, <span style="color:#ba2121">"age"</span>);
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>
登录后复制
输出:
<code>
<code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Hello Kevin
</span></span><span style="display:flex;"><span>Hello Kevin 23
</span></span></code></code>
登录后复制

static 类和 call_user_funcstatic 方法实现为 callback 在 PHP 中使用

函数

static 方法创建两个 static 类,并使用 call_user_func() 方法将它们作为 callbacks 我们将使用

执行。

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">// Sample Person class
</span></span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Person</span> {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">static</span> <span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">walking</span>() {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"I am moving my feet <br>"</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//child class extends the parent Person class
</span></span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Student</span> <span style="color:#008000;font-weight:bold">extends</span> Person {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">static</span> <span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">walking</span>() {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"student is moving his/her feet <br>"</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">// Parent class Static method callbacks
</span></span></span><span style="display:flex;"><span>call_user_func(<span style="color:#008000;font-weight:bold">array</span>(<span style="color:#ba2121">'Person'</span>, <span style="color:#ba2121">'walking'</span>));
</span></span><span style="display:flex;"><span>call_user_func(<span style="color:#ba2121">'Person::walking'</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">// Child class Static method callback
</span></span></span><span style="display:flex;"><span>call_user_func(<span style="color:#008000;font-weight:bold">array</span>(<span style="color:#ba2121">'Student'</span>, <span style="color:#ba2121">'Student::walking'</span>));
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>
登录后复制
输出:🎜
<code>
<code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>I am moving my feet
</span></span><span style="display:flex;"><span>I am moving my feet
</span></span><span style="display:flex;"><span>student is moving his/her feet
</span></span></code></code>
登录后复制

以上是在 PHP 中实现回调函数的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:lsjlt.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板