天天看點

php深入學習筆記二( 函數内置函數 )

1. call_​user_​func_​array

調用使用者自定義函數,第一個參數是函數名,

第二個參數是函數的參數 必須是是一索引數組

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_user_func_array("foobar", array("one", "two"));


// 類成員函數調用
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
           

2. call_​user_​func

調用函數 參數不能傳遞引用 參數

call_user_func(函數名,參數1,參數2...)

call_user_func(function($arg) { print "[$arg]\n"; }, 'test');
           

3. create_​function 

建立一個匿名函數

$myfunc = create_​function('函數參數','函數體');

$myfunc(函數參數);

$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
           

4. forward_​static_​call_​array

調用一個靜态函數 方式同 call_user_func_array

5. forward_​static_​call

調用一個靜态函數 方式同 call_user_func

6. func_​get_​arg(index) 

 傳回參數清單的某一項 參數為索引值

7. func_​get_​args 

 以數組形式收集所有參數

<?php
function sum() {
    $acc = 0;
    foreach (func_get_args() as $n) {
        $acc += $n;
    }
    return $acc;
}


echo sum(1, 2, 3, 4);
?>
           

8. func_​num_​args() 

 傳回函數傳遞參數的個數

9. function_​exists  

檢測函數是否存在

function_​exists("函數名"); // 檢測一個函數是否存在 

10. get_​defined_​functions 

以二維數組形式傳回所有定義過的函數

包括系統函數    (internal)

和使用者自定義函數(user)

Array
(
    [internal] => Array
        (
            [0] => zend_version
            [1] => func_num_args
            [2] => func_get_arg
            [3] => func_get_args
            [4] => strlen
            [5] => strcmp
            [6] => strncmp
            ...
            [750] => bcscale
            [751] => bccomp
        )


    [user] => Array
        (
            [0] => myrow
        )


)
           

11. register_​shutdown_​function

 該函數注冊的函數 ,在系統執行超過最大時間Fatal error時

 仍會執行 注冊的函數 

function add(){
    code here...
 }
 register_​shutdown_​function("add");