Home > Backend Development > PHP Tutorial > Implementing callback functions in PHP

Implementing callback functions in PHP

WBOY
Release: 2024-02-28 10:10:01
forward
521 people have browsed it

In PHP, callback function is a common programming technology that can enhance the flexibility and reusability of code. Through callback functions, we can pass functions as parameters to other functions to be called under specific conditions. This approach makes the code structure clearer and allows the function's behavior to be dynamically changed as needed. This article will introduce how to implement callback functions in PHP, and how to effectively use callback functions to simplify code logic and improve code maintainability. PHP editor Baicao will explain to you in detail how to use callback functions, allowing you to easily master this important programming technology.


Create a callback function in php and execute using call_user_func

We created a callback<strong class="keylink"> function named </strong>testFunction() and used the call_user_func() method by giving the function name as String Passed to this method to execute it.

example:

<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>
Copy after login

Output:

<code>
<code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Testing Callback
</span></span></code></code>
Copy after login

Create a callback function in PHP and execute using the array_map

method

We use the array_map method to execute the callback function. This will execute the method using the corresponding data passed to the array_map() function.

example:

<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>
Copy after login

Output:

<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>
Copy after login

Implement multiple callback functions in PHP and execute them using user-defined functions

We will use a user-defined function named testCallBacks() to execute two callback functions named name and age. The name as a string bypasses the user-defined function.

example:

<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>
Copy after login

Output:

<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>
Copy after login

Implement the static method as a callback function in PHP using the static class and call_user_func

We will create two static classes using the static method and execute them as callbacks using the call_user_func() method.

<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>
Copy after login

Output:

<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>
Copy after login

The above is the detailed content of Implementing callback functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template