搜尋
首頁後端開發php教程php函數call_user_func和call_user_func_array用法實例詳解

看UCenter的時候有一個函數call_user_func,百思不得其解,因為我以為是自己定義的函數,結果到處都找不到,後來百度了一下才知道call_user_func是內建函數

call_user_func函數類似於特別的呼叫函數的方法,使用方法如下:

function a($b,$c) 
{ 
echo $b; 
echo $c; 
} 
call_user_func('a', "111","222"); 
call_user_func('a', "333","444"); 
//显示 111 222 333 444

呼叫類別內部的方法比較奇怪,居然用的是array,不知道開發者是如何考慮的,當然省去了new,也是滿有新意的: 

class a { 
function b($c) 
{ 
echo $c; 
} 
} 
call_user_func(array("a", "b"),"111"); 
//显示 111

call_user_func_array函數和call_user_func很相似,只不過是換了一種方式傳遞了參數,讓參數的結構更清晰: 

function a($b, $c) 
{ 
echo $b; 
echo $c; 
} 
call_user_func_array('a', array("111", "222")); 
//显示 111 222

call_user_func_array函數也可以呼叫類別內部的方法的 

Class ClassA 
{ 
function bc($b, $c) { 
$bc = $b + $c; 
echo $bc; 
} 
} 
call_user_func_array(array('ClassA','bc'), array("111", "222")); 
//显示 333

call_user_func函數和call_user_func_array函數都支援#引用,這讓他們和普通的函數呼叫更趨於功能一致: 

function a(&$b) 
{ 
$b++; 
} 
$c = 0; 
call_user_func('a', &$c); 
echo $c;//显示 1 
call_user_func_array('a', array(&$c)); 
echo $c;//显示 2

php之call_user_func_array的簡易用法

<?php 
function foobar($arg, $arg2) { 
echo FUNCTION, " got $arg and $arg2\n"; 
} 
class foo { 
function bar($arg, $arg2) { 
echo METHOD, " got $arg and $arg2\n"; 
} 
} 
// Call the foobar() function with 2 arguments 
call_user_func_array("foobar", array("one", "two")); 
// Call the $foo->bar() method with 2 arguments 
$foo = new foo; 
call_user_func_array(array($foo, "bar"), array("three", "four")); 
?>

以上程式的輸出類似於:
foobar got one and two
foo::bar got three and four
Example #2 call_user_func_array() using namespace name 

<?php 
namespace Foobar; 
class Foo { 
static
 public function test($name) { 
print "Hello {$name}!\n"; 
} 
} 
// As of PHP 5.3.0 
call_user_func_array(NAMESPACE .&#39;\Foo::test&#39;, array(&#39;Hannes&#39;)); 
// As of PHP 5.3.0 
call_user_func_array(array(NAMESPACE .&#39;\Foo&#39;, &#39;test&#39;), array(&#39;Philip&#39;)); 
?>

以上例程的輸出類似於:
Hello Hannes!
Hello Philip!
Example #3 Using lambda function 

<?php 
$func = function($arg1, $arg2) { 
return $arg1 * $arg2; 
}; 
var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.3.0 */ 
?>

以上例程會輸出:
int(8)

以上是php函數call_user_func和call_user_func_array用法實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
使用PHP發送電子郵件的最佳方法是什麼?使用PHP發送電子郵件的最佳方法是什麼?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依賴注入的最佳實踐PHP中依賴注入的最佳實踐May 08, 2025 am 12:21 AM

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

PHP性能調整技巧和技巧PHP性能調整技巧和技巧May 08, 2025 am 12:20 AM

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

PHP電子郵件安全性:發送電子郵件的最佳實踐PHP電子郵件安全性:發送電子郵件的最佳實踐May 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

您如何優化PHP應用程序的性能?您如何優化PHP應用程序的性能?May 08, 2025 am 12:08 AM

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,數據庫imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)優化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

PHP中的依賴注入是什麼?PHP中的依賴注入是什麼?May 07, 2025 pm 03:09 PM

依賴性注射inphpisadesignpatternthatenhancesFlexibility,可檢驗性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

最佳PHP性能優化技術最佳PHP性能優化技術May 07, 2025 pm 03:05 PM

PHP性能優化可以通過以下步驟實現:1)在腳本頂部使用require_once或include_once減少文件加載次數;2)使用預處理語句和批處理減少數據庫查詢次數;3)配置OPcache進行opcode緩存;4)啟用並配置PHP-FPM優化進程管理;5)使用CDN分發靜態資源;6)使用Xdebug或Blackfire進行代碼性能分析;7)選擇高效的數據結構如數組;8)編寫模塊化代碼以優化執行。

PHP性能優化:使用OpCode緩存PHP性能優化:使用OpCode緩存May 07, 2025 pm 02:49 PM

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版