PHP - 从函数中获取所有参数(甚至是可选参数)

时间:2022-01-30 08:44:02

I want to get all parameters (passed or not) from a function.

我想从函数中获取所有参数(传递或不传递)。

Example:

例:

<?php
    function foo($a, $b=1)
    {
         return $a-$b;
    }
?>

If I call

如果我打电话

$test = func_get_args(foo(10));
var_dump($test);

I will have only an array with [0] => 10.

我将只有一个[0] => 10的数组。

How can I have the value(s) of the optional parameter(s) even if I don’t pass it/them? (I know that func_get_args only returns passed parameters.)

即使我没有传递它/它们,我怎么能得到可选参数的值? (我知道func_get_args只返回传递的参数。)

EDIT: To be more precise, here is what I’m doing:

编辑:更确切地说,这就是我正在做的事情:

function insertLog($fct_name="-1", $type="-1", $error="-1", ....)
{
     // first thing
     $params = func_get_args();
     var_dump($params);
}

2 个解决方案

#1


10  

You can accomplish this using the ReflectionFunction function class.

您可以使用ReflectionFunction函数类完成此操作。

function foo($a, $b=1)
{
    $arr = array();
    $ref = new ReflectionFunction(__FUNCTION__);
    foreach($ref->getParameters() as $parameter)
    {
        $name = $parameter->getName();
        $arr[$name] = ${$name};
    }
    print_r($arr);

    // ...
}

Calling the function:

调用功能:

foo(1);

Output:

输出:

Array
(
    [a] => 1
    [b] => 1
)

Demo

演示

#2


1  

func_get_args

func_get_args

Note: This function returns a copy of the passed arguments only, and does not account for default (non-passed) arguments.

注意:此函数仅返回传递的参数的副本,并不考虑默认(未传递)参数。

Ive created a function called func_get_all_args which takes advantage of Reflection. It returns the same array as func_get_args but includes any missing default values.

我创建了一个名为func_get_all_args的函数,它利用了Reflection。它返回与func_get_args相同的数组,但包含任何缺少的默认值。

function func_get_all_args($func, $func_get_args = array()){

    if((is_string($func) && function_exists($func)) || $func instanceof Closure){
        $ref = new ReflectionFunction($func);
    } else if(is_string($func) && !call_user_func_array('method_exists', explode('::', $func))){
        return $func_get_args;
    } else {
        $ref = new ReflectionMethod($func);
    }
    foreach ($ref->getParameters() as $key => $param) {

        if(!isset($func_get_args[ $key ]) && $param->isDefaultValueAvailable()){
            $func_get_args[ $key ] = $param->getDefaultValue();
        }
    }
    return $func_get_args;
}

Usage

用法

function my_function(){

    $all_args = func_get_all_args(__FUNCTION__, func_get_args());
    call_user_func_array(__FUNCTION__, $all_args);
}

public function my_method(){

    $all_args = func_get_all_args(__METHOD__, func_get_args());
    // or
    $all_args = func_get_all_args(array($this, __FUNCTION__), func_get_args());

    call_user_func_array(array($this, __FUNCTION__), $all_args);
}

This could probably do with a bit of improvement such ac catching and throwing errors.

这可能会带来一些改进,例如ac捕捉和抛出错误。

#1


10  

You can accomplish this using the ReflectionFunction function class.

您可以使用ReflectionFunction函数类完成此操作。

function foo($a, $b=1)
{
    $arr = array();
    $ref = new ReflectionFunction(__FUNCTION__);
    foreach($ref->getParameters() as $parameter)
    {
        $name = $parameter->getName();
        $arr[$name] = ${$name};
    }
    print_r($arr);

    // ...
}

Calling the function:

调用功能:

foo(1);

Output:

输出:

Array
(
    [a] => 1
    [b] => 1
)

Demo

演示

#2


1  

func_get_args

func_get_args

Note: This function returns a copy of the passed arguments only, and does not account for default (non-passed) arguments.

注意:此函数仅返回传递的参数的副本,并不考虑默认(未传递)参数。

Ive created a function called func_get_all_args which takes advantage of Reflection. It returns the same array as func_get_args but includes any missing default values.

我创建了一个名为func_get_all_args的函数,它利用了Reflection。它返回与func_get_args相同的数组,但包含任何缺少的默认值。

function func_get_all_args($func, $func_get_args = array()){

    if((is_string($func) && function_exists($func)) || $func instanceof Closure){
        $ref = new ReflectionFunction($func);
    } else if(is_string($func) && !call_user_func_array('method_exists', explode('::', $func))){
        return $func_get_args;
    } else {
        $ref = new ReflectionMethod($func);
    }
    foreach ($ref->getParameters() as $key => $param) {

        if(!isset($func_get_args[ $key ]) && $param->isDefaultValueAvailable()){
            $func_get_args[ $key ] = $param->getDefaultValue();
        }
    }
    return $func_get_args;
}

Usage

用法

function my_function(){

    $all_args = func_get_all_args(__FUNCTION__, func_get_args());
    call_user_func_array(__FUNCTION__, $all_args);
}

public function my_method(){

    $all_args = func_get_all_args(__METHOD__, func_get_args());
    // or
    $all_args = func_get_all_args(array($this, __FUNCTION__), func_get_args());

    call_user_func_array(array($this, __FUNCTION__), $all_args);
}

This could probably do with a bit of improvement such ac catching and throwing errors.

这可能会带来一些改进,例如ac捕捉和抛出错误。