使用数组作为PHP函数中的默认参数

时间:2022-03-30 17:08:48

Is it possible to use an array as a default parameter in a PHP function?

是否可以在PHP函数中使用数组作为默认参数?

I would like to do the following:

我想做以下事情:

$arr = array(1,2,3,4);

function sample($var1, $var2, $var3 = $arr){
    echo $var1.$var2;
    echo print_r($var3);
}

sample('a','b');
// Should still work without the third parameter and default to $arr

2 个解决方案

#1


21  

No, this is not possible, the right hand expression of the default value must be a constant or array literal, i.e.

不,这是不可能的,默认值的右手表达式必须是常量或数组文字,即

function sample($var1, $var2, $var3 = array(1, 2, 3, 4))
{
}

If you want this behaviour, you could use a closure:

如果您想要这种行为,可以使用闭包:

$arr = array(1, 2, 3, 4);

$sample = function ($var1, $var2, array $var3 = null) use ($arr) {
    if (is_null($var3)) {
        $var3 = $arr;
    }

    // your code
}

$sample('a', 'b');

You could also express it with a class:

你也可以用一个类来表达它:

class Foo
{
    private static $arr = array(1, 2, 3, 4);

    public static function bar($var1, $var2, array $var3 = null)
    {
        if (is_null($var3)) {
            $var3 = self::$arr;
        }

        // your code here
    }
}

Foo::bar('a', 'b');

#2


7  

You can't pass $arr into the function definition, you'll have to do:

你不能将$ arr传递给函数定义,你必须这样做:

function sample($var1, $var2, $var3 = array('test')){
    echo $var1.$var2;
    echo print_r($var3);
}

sample('a','b'); // output: abArray ( [0] => test ) 1

or, if you want to be really dirty (I wouldn't recommend it..):

或者,如果你想变得很脏(我不推荐它......):

$arr = array('test');

function sample($var1, $var2, $var3 = null){
    if($var3 == null){
      global $arr;
      $var3 = $arr;
    }
    echo $var1.$var2;
    echo print_r($var3);
}

sample('a','b');

#1


21  

No, this is not possible, the right hand expression of the default value must be a constant or array literal, i.e.

不,这是不可能的,默认值的右手表达式必须是常量或数组文字,即

function sample($var1, $var2, $var3 = array(1, 2, 3, 4))
{
}

If you want this behaviour, you could use a closure:

如果您想要这种行为,可以使用闭包:

$arr = array(1, 2, 3, 4);

$sample = function ($var1, $var2, array $var3 = null) use ($arr) {
    if (is_null($var3)) {
        $var3 = $arr;
    }

    // your code
}

$sample('a', 'b');

You could also express it with a class:

你也可以用一个类来表达它:

class Foo
{
    private static $arr = array(1, 2, 3, 4);

    public static function bar($var1, $var2, array $var3 = null)
    {
        if (is_null($var3)) {
            $var3 = self::$arr;
        }

        // your code here
    }
}

Foo::bar('a', 'b');

#2


7  

You can't pass $arr into the function definition, you'll have to do:

你不能将$ arr传递给函数定义,你必须这样做:

function sample($var1, $var2, $var3 = array('test')){
    echo $var1.$var2;
    echo print_r($var3);
}

sample('a','b'); // output: abArray ( [0] => test ) 1

or, if you want to be really dirty (I wouldn't recommend it..):

或者,如果你想变得很脏(我不推荐它......):

$arr = array('test');

function sample($var1, $var2, $var3 = null){
    if($var3 == null){
      global $arr;
      $var3 = $arr;
    }
    echo $var1.$var2;
    echo print_r($var3);
}

sample('a','b');