The pass value for the last parameter of the default function

Since a very long time I have been working on php.

But one question, maybe I have no idea about

as i have one function like below:

function hello($param1, $param2="2", $param3="3", $param4="4")

Now, whenever I use this function, and if I need the 4th parameters, which are the $ param4 parameter, then I still need to call everything as empty, like this:

hello(1, '', '', "param4");

So, is there any other way to just pass the 1st and 4th parameters in the call, rather than a long list of spaces?

Or is there any other standard way to do this?

+4
source share
4 answers

There was an RFC for this skipparams name , but it was rejected.

PHP , hello(1, , , "param4"); hello(1, default, default, "param4"); ( RFC) .

, jQuery :

function hello( $param1, $more_params = [] )
{
    static $default_params = [
        'param2' => '2',
        'param3' => '3',
        'param4' => '4'
    ];

    $more_params = array_merge( $default_params, $more_params );
}

:

hello( 1, [ 'param4'=>'not 4, muahaha!' ] );

, , array_merge() $more_params .

+5

, , , .

, , .

function hello($param1, $variables = ["param2" => "2", "param3" => "3", "param4" => "4"]) {
    if(!array_key_exists("param2", $variables)) $variables['param2'] = "2";
    if(!array_key_exists("param3", $variables)) $variables['param3'] = "3";
    if(!array_key_exists("param4", $variables)) $variables['param4'] = "4";

    echo "<pre>".print_r($variables, true)."</pre>";
}

"param4" , .

:

hello("test", ["param4" => "filling in variable 4"]);

:

Array
(
    [param4] => filling in variable 4
    [param2] => 2
    [param3] => 3
)

, , , .

, , .

0

, , - .

, .

, . , ;)

:

function some_call($parm1, $parm2='', $parm3='', $parm4='') { ... }

:

function some_call_4($parm1, $parm4) {
    return some_call($parm1, '', '', $parm4);
}

, ALOT , .

, , .

0

This is an overhead, but you can use it ReflectionFunctionto create a class that you can call an instance using named parameters

final class FunctionWithNamedParams
{
    private $func;

    public function __construct($func)
    {
        $this->func = $func;
    }

    public function __invoke($params = [])
    {
        return ($this->func)(...$this->resolveParams($params));
    }

    private function resolveParams($params)
    {
        $rf = new ReflectionFunction($this->func);

        return array_reduce(
            $rf->getParameters(),
            function ($carry, $param) use ($params) {
                if (isset($params[$param->getName()])) {
                    $carry[] = $params[$param->getName()];
                } else if ($param->isDefaultValueAvailable()) {
                    $carry[] = $param->getDefaultValue();
                } else {
                    throw new BadFunctionCallException;
                }

                return $carry;
            },  
            []
        );
    }
}

Then you can use it as follows:

function hello($param1, $param2 = "2", $param3 = "3", $param4 = "4")
{
    var_dump($param1, $param2, $param3, $param4);
}

$func = new FunctionWithNamedParams('hello');

$func(['param1' => '1', 'param4' => 'foo']);

Here is a demo .

-1
source

Source: https://habr.com/ru/post/1693434/


All Articles