php 函数

Table of Contents

php函数,每天温习,就进步了

call_user_func_array and call_user_func

//调用自定义函数
function test($arg1, $arg2)
{
    echo __FUNCTION__, ":$arg1, $arg2\n";
}

class A {
    function test($arg1, $arg2)
    {
        echo __FUNCTION__, ":$arg1, $arg2\n";
    }    
}

call_user_func_array("test", array("josephzeng", "30"));

$a = new A();
call_user_func_array(array($a, "test"), array("josephzeng", "30"));

call_user_func('test', 'josephzeng', "31");

输出
test:josephzeng, 30
test:josephzeng, 30
test:josephzeng, 31

func_get_args and func_num_args

function test() {
    //以数组形式获取所有参数
    print_r(func_get_args());
    echo "\n";
    //获取参数个数
    echo func_num_args();
}

test("abc", "123", "456", "hhh");

输出
Array
(
    [0] => abc
    [1] => 123
    [2] => 456
    [3] => hhh
)

4

Author: josephzeng

Last Updated 2016-04-11. Created by Emacs 24.5.1 (Org mode 8.2.10)

Validate