在PHP中,回呼函數是一種常見的程式技術,能夠增強程式碼的靈活性和可重複使用性。透過回調函數,我們可以將函數作為參數傳遞給其他函數,在特定的條件下被呼叫。這種方式使得程式碼結構更加清晰,並且可以根據需要動態地改變函數的行為。本文將介紹在PHP中實作回調函數的方法,以及如何有效地利用回調函數來簡化程式碼邏輯和提高程式碼的可維護性。 php小編百草將為您詳細講解如何使用回呼函數,讓您輕鬆掌握這項重要的程式設計技術。
callback
函數並使用 call_user_func
執行
我們建立了一個名為testFunct<strong class="keylink">io</strong>n()
的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">'testFunction'</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
方法執行
我們使用 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>
我們將使用名為testCallBacks()
的使用者定義函數執行兩個名為name
和age
的callback
函數,將函數的名稱作為字串繞過使用者定義的函數。
範例:
<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_func
將 static
方法實作為 callback
函數
我們將使用 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中文網其他相關文章!