PHP Conference Japan 2024

ReflectionFunction::getClosure

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

ReflectionFunction::getClosure傳回函式的動態建立的閉包

說明

public ReflectionFunction::getClosure(): Closure

取得函式的動態建立的閉包。

參數

此函式沒有參數。

回傳值

傳回新建立的 Closure

另請參閱

新增註解

使用者貢獻的註記 1 則註記

php dot net-bug at spamhereplease dot com
11 年前
如果您有一個會經常被呼叫的回呼函式,您可以使用這個方法(以及 ReflectProperty 的相關方法)來取得一個執行速度接近原生速度的閉包。

一些在 Ubuntu 上使用 PHP 5.4.22 進行的基準測試比較

[函式呼叫] [時間] [% 較慢]
function(): 0.09527 0%
Reflection::Closure: 0.09729 2%
$string(): 0.102641 8%
Reflection: 0.197122 107%
call_user_func: 0.214045 125%

[方法呼叫] [時間] [% 較慢] [% 與函式比較]
->method(): 0.103611 0% 9%
Reflection::Closure: 0.1043 1% 9%
$string(): 0.109885 6% 15%
Reflection: 0.20451 97% 115%
call_user_func: 0.219205 112% 130%
To Top