PHP 匿名函數

王林
發布: 2024-08-29 12:47:23
原創
447 人瀏覽過

可以在沒有任何特定名稱的情況下建立並用作 PHP 腳本中的輸入參數的函數,稱為匿名函數。這些功能是使用 Closure 類別實現的。將匿名函數指派給變數的過程與任何其他分配語法相同。透過將變數從父作用域傳遞到 use 語言構造,子作用域中的匿名函數可以繼承該變數。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

匿名函數不帶有任何名稱:

function($arg1,$arg2,….,$argN){
//The definition for the anonymous function
};
登入後複製

不同類型的用例

透過使用匿名函數開發有效的 PHP 編碼可以實現多種目標。匿名函數根據使用該函數的不同類型的用例表現出不同的功能。

下面給了五個主要用例:

1.用例 1

匿名函數可用來為變數賦值。它遵循與其他賦值操作相同的語法。

範例

下面的程式碼片段用於將給定的輸入值指派給輸出值,並使用輸出變數列印該值。

代碼:

<?php
$Var = function($value) //Anonymous function is used to assign value to variable $Var
{
//Anonymous function definition
printf("The assigned value is: %s\r\n", $value);
};
//Calling the anonymous function using the assigning variable $Var with a string value input
$Var('A string value is assigned');
//Calling the anonymous function using the assigning variable $Var with a integer value input
$Var(35);
?>
登入後複製

輸出:

透過匿名函數呼叫列印給定的字串和整數類型的輸入值,如下所示:

PHP 匿名函數

2.用例 2

定義匿名函數的特性對於建立內聯回呼函數起著重要作用。

在這種情況下,匿名函數可以作為輸入參數傳遞給另一個函數。

下面的程式碼用來定義回呼函數 preg_replace_callback。

將匿名函數作為其輸入參數之一。

代碼:

<?php
//creating callback function using PHP anonymous function
// preg_replace_callback is the calling function
echo preg_replace_callback('~-([a-z])~', function ($input) {
//Anonymous function definition
return strtoupper($input[1]);
}, 'This is an anonymous callback function!');//End of the definition of the callback function
?>
登入後複製

輸出:

執行 PHP 腳本時,會觸發回呼函數,而匿名函數的輸出將列印在輸出視窗上,如下所示:

PHP 匿名函數

3.用例 3

匿名函數可用來從父作用域繼承變數。

此用例不支援超級全域變數、$this 變數或任何同名參數變數。

範例

代碼:

<?php
$input_text = 'Initial message';
$outputVar = function () {
//Anonymous function definition
var_dump($input_text);
};
$outputVar();
// Inherit the variable $input_text by value
$outputVar = function () use ($input_text) {
var_dump($input_text);
};
$outputVar();
// Inherit the variable $input_text by-reference
$outputVar = function () use (&$input_text) {
var_dump($input_text);
};
$outputVar();
// Modifying the variable value of parent scope from the function call
$input_text = ' Next message';
$outputVar();
// Inserting regular argument along with parent scope variable
$outputVar = function ($arg) use ($input_text) {
var_dump($arg . ' ' . $input_text);
};
$outputVar("Random message");
?>
登入後複製

輸出:

上述程式碼的結果輸出如下:

PHP 匿名函數

4.用例 4

從 PHP 5.4 版本開始,宣告任何類別時,該類別預設綁定匿名函數功能。這使得變數“$this”在類別中定義的任何匿名函數的範圍內可用。

範例

代碼:

<?php
class AnonymousClass
{
public function Classfunction()
{
return function() {
var_dump($this); //Retrieves the dump information of class object using $this variable,once //it is created
};
}
}
$Classobject = new AnonymousClass;
$function = $Classobject->Classfunction();
$function();
?>
登入後複製

輸出:

來自類別「AnonymousClass」的物件的轉儲資訊列印在輸出視窗上,如下所示:

PHP 匿名函數

5.用例 5

在建立物件時,如果從相同物件的範圍實例化閉包並註冊,它將建立一個循環引用,從而防止立即銷毀該物件。應用靜態匿名函數可以使腳本克服延遲。

下面的範例示範了常規匿名函數和靜態匿名函數的使用對比分析。

範例:

情況1:不使用靜態匿名函數

代碼:

<?php
class TrialClass
{
private $AnonymousVar;
public function __construct()
{
$this->AnonymousVar = function () {
};
}
public function __destruct()
{
echo "Destruction function is called";
}
}
new TrialClass;
echo "After the object is being defined";
echo "\n";
?>
登入後複製

輸出:

PHP 匿名函數

情況 2:包含靜態匿名函數

代碼:

<?php
class TrialClass
{
private $AnonymousVar;
public function __construct()
{
$this->closure = self::createClosure();
}
public static function createClosure()
{
return function () {
};
}
public function __destruct()
{
echo "Destruction function is called";
}
}
new TrialClass;
echo "\n";
echo "\n";
echo "After the object is being defined";
echo "\n";
echo "\n";
?>
登入後複製

輸出:

PHP 匿名函數

補充說明

  • 自動將目前類別綁定到匿名函數是 PHP 5.4 版及以上版本的預設行為。這可以透過實作靜態匿名函數來禁止。
  • 它增強了效能,因為它可用於定義僅使用一次的函數。匿名函數僅適用於需要從該函數執行的作業,且不適用於其餘程式碼。
  • 當使用匿名函數為變數賦值時,PHP 會自動將表達式轉換為 Closure 內部類別實例。

以上是PHP 匿名函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!