PHP中功能強大卻少使用的函數 — 為你提供更多的思路

來源:互聯網
上載者:User

call_user_func_array — 讓參數以數組的形式調用一個函數
call_user_func — 調用一個存在的函數
create_function — 建立一個函數
func_get_arg — 擷取函數中某個參數的值
func_get_args — 擷取函數的所有參數並組成數組
func_num_args — 擷取一個函數的參數個數
function_exists — 判定一個函數是否存在
get_defined_functions — 擷取已有的函數資訊
register_shutdown_function — 註冊一個頁面載入完成後啟動並執行函數
register_tick_function — 註冊一個按要求調用的函數
unregister_tick_function — 取消一個按要求調用的函數

get_defined_functions可以擷取所有的PHP函數和自訂的函數:

  1. function a(){}   
  2. $b = get_defined_functions();   
  3. print_r($b);   
  4. //也許會顯示1000多個已定義了的函數:)   
  5. ?>  

<?php   

<?phpfunction a(){}$b = get_defined_functions();print_r($b);//也許會顯示1000多個已定義了的函數:)?>

function_exists函數判定一個函數是否存在(可以是PHP函數,也可以是自訂函數)。

  1. if (function_exists('a')) {   
  2.     echo "yes";   
  3. } else {   
  4.     echo "no";   
  5. }   
  6. function a(){}   
  7. // 顯示 yes   
  8. ?>  

<?php   

<?phpif (function_exists('a')) {    echo "yes";} else {    echo "no";}function a(){}// 顯示 yes?>

call_user_func函數類似於一種特別的調用函數的方法,使用方法如下:

  1. function a($b,$c)   
  2. {   
  3.     echo $b;   
  4.     echo $c;   
  5. }   
  6. call_user_func('a', "111","222");   
  7. call_user_func('a', "333","444");   
  8. //顯示 111 222 333 444   
  9. ?>  

<?php   

<?phpfunction 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,也是滿有新意的:

  1. <?php   
  2. class a {   
  3.     function b($c)   
  4.      {   
  5.         echo $c;   
  6.      }   
  7. }   
  8. call_user_func(array("a", "b"),"111");   
  9. //顯示 111   
  10. ?>  
<?phpclass a {    function b($c)    {        echo $c;    }}call_user_func(array("a", "b"),"111");//顯示 111?>

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

  1. function a($b, $c)   
  2. {   
  3.     echo $b;   
  4.     echo $c;   
  5. }   
  6. call_user_func_array('a', array("111", "222"));   
  7. //顯示 111 222   
  8. ?>  

<?php   

<?phpfunction a($b, $c){    echo $b;    echo $c;}call_user_func_array('a', array("111", "222"));//顯示 111 222?>

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

  1. function a(&$b)   
  2. {   
  3.     $b++;   
  4. }   
  5. $c = 0;   
  6. call_user_func('a', &$c);   
  7. echo $c;//顯示 1   
  8. call_user_func_array('a', array(&$c));   
  9. echo $c;//顯示 2   
  10. ?>  

<?php   

<?phpfunction a(&$b){    $b++;}$c = 0;call_user_func('a', &$c);echo $c;//顯示 1call_user_func_array('a', array(&$c));echo $c;//顯示 2?>


func_num_args函數可以擷取函數接受到參數的數量:

  1. function a()   
  2. {   
  3.     echo func_num_args();   
  4. }   
  5. a(111, 222, 333);   
  6. //顯示 3   
  7. ?>  

<?php   

<?phpfunction a(){    echo func_num_args();}a(111, 222, 333);//顯示 3?>

func_get_arg函數可以擷取某一個傳遞過來參數的值,在下面的例子中,在函數中並沒有具體說明有哪些參數會被接受,利用 func_get_arg還可以擷取額外的參數:

  1. function a()   
  2. {   
  3.      echo func_get_arg(1);   
  4. }   
  5. a (111, 222, 333);   
  6. //顯示 222   
  7. ?>  

<?php   

<?phpfunction a(){     echo func_get_arg(1);}a (111, 222, 333);//顯示 222?>


func_get_args函數的作用和func_get_arg非常相似,是把所有的參數當成數組來調用:

  1. function a()   
  2. {   
  3.     $numargs = func_num_args();   
  4.     $b = func_get_args();   
  5.     for ($i = 0; $i < $numargs; $i++) {   
  6.         echo $b[$i];   
  7.      }   
  8. }   
  9. a(111, 222, 333);   
  10. //顯示 111 222 333   
  11. ?>  

<?php   

<?phpfunction a(){    $numargs = func_num_args();    $b = func_get_args();    for ($i = 0; $i < $numargs; $i++) {        echo $b[$i];    }}a(111, 222, 333);//顯示 111 222 333?>

create_function函數可以建立一個匿名的函數(函數名被PHP預設為lambda_1,lambda_2),樣子比較古怪,但是形式比較奇特,要注意第二個參數內的語句要有“;”分隔:

  1. $newfunc = create_function('$a,$b', 'return $a + $b;');   
  2. echo $newfunc;   
  3. echo $newfunc(2, 3);   
  4. //顯示 lambda_1 5   
  5. ?>  

<?php   

<?php$newfunc = create_function('$a,$b', 'return $a + $b;');echo $newfunc;echo $newfunc(2, 3);//顯示 lambda_1 5?>

register_shutdown_function函數可以註冊一個在頁面載入完成之後啟動並執行函數(功能有點像緩衝),register_shutdown_function也可以像call_user_func函數一樣用作對類內部方法的調用:

  1. function a() {   
  2.    echo   222 ;   
  3. }   
  4. echo 111;   
  5. register_shutdown_function('a');   
  6. //顯示 111 222   
  7. ?>   

<?php   

<?phpfunction a() {   echo  222 ;}echo 111;register_shutdown_function('a');//顯示 111 222?>
  1. class a   
  2. {   
  3.    function b ($c)   
  4.     {   
  5.       echo $c;   
  6.     }   
  7. }   
  8. register_shutdown_function (array ('a', 'b'), '111');   
  9. //顯示 111   
  10. ?>  

<?php   

<?phpclass a{   function b ($c)   {      echo $c;   }}register_shutdown_function (array ('a', 'b'), '111');//顯示 111?>


register_tick_function函數和unregister_tick_function函數必須要和declare流程式控制制機制合并使用,那麼就先瞭解一下declare和tick:

  1. statement   
  2. }  

declare (directive){   

declare (directive){statement}

Tick 是一個在 declare 程式碼片段中解譯器每執行 N 條低級語句就會發生的事件。N 的值是在 declare 中的 directive 部分用 ticks=N 來指定的。在每個 tick 中出現的事件是由 register_tick_function() 來指定的。舉例如下:

  1. function foo($str) {   
  2. static $i = 0;   
  3. print "$str: $i<br>";   
  4. $i++;   
  5. }   
  6. register_tick_function("foo", "count");   
  7. declare (ticks = 6) {   
  8. for($i=0; $i<20; $i++) {   
  9. echo "$i<br>";   
  10. }   
  11. }   
  12. ?>  

<?   

<?function foo($str) {static $i = 0;print "$str: $i<br>";$i++;}register_tick_function("foo", "count");declare (ticks = 6) {for($i=0; $i<20; $i++) {echo "$i<br>";}}?>

在這個例子中的declare(ticks = N){statement}流程式控制制裡面,每執行6行(ticks = 6)代碼,就需要運行一次foo()函數,通過更改ticks的值,我們可以獲得不同的運行結果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.