Heim > Artikel > Backend-Entwicklung > Callback-Funktionen in PHP implementieren
In PHP ist die Callback-Funktion eine gängige Programmiertechnik, die die Flexibilität und Wiederverwendbarkeit von Code verbessern kann. Durch Rückruffunktionen können wir Funktionen als Parameter an andere Funktionen übergeben, die unter bestimmten Bedingungen aufgerufen werden. Dieser Ansatz macht die Codestruktur klarer und ermöglicht die dynamische Änderung des Verhaltens der Funktion nach Bedarf. In diesem Artikel erfahren Sie, wie Sie Rückruffunktionen in PHP implementieren und wie Sie Rückruffunktionen effektiv nutzen, um die Codelogik zu vereinfachen und die Wartbarkeit des Codes zu verbessern. Der PHP-Editor Baicao erklärt Ihnen ausführlich die Verwendung von Callback-Funktionen, sodass Sie diese wichtige Programmiertechnologie problemlos beherrschen können.
callback
函数并使用 call_user_func
php
Wir haben eine callback
-Funktion namens testFunct<code>testFunct<strong class="keylink">io</strong>n()
的 callback
函数,并使用 call_user_func()
ion() erstellt und die Methode call_user_func()
verwendet, indem wir den Funktionsnamen als Die Zeichenfolge angegeben haben
Beispiel:
<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">'testFunction'</span>); </span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span > </span></span></span></code></code>Ausgabe:
<code> <code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Testing Callback </span></span></code></code>
callback
函数并使用 array_map
Erstellen Sie eine
array_map
方法执行 callback
函数。这将使用传递给 array_map()
Wir führen diese Methode mit den entsprechenden Daten der
Beispiel:
<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>Ausgabe:
<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>
testCallBacks()
的用户定义函数执行两个名为 name
和 age
的 callback
Wir werden eine Funktion namens
Beispiel:
<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>Ausgabe:
<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_func
将 static
方法实现为 callback
Verwenden von in PHP
static
方法创建两个 static
类,并使用 call_user_func()
方法将它们作为 callbacks
Wir werden
<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>Ausgabe: 🎜
<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>
Das obige ist der detaillierte Inhalt vonCallback-Funktionen in PHP implementieren. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!