0

What I want to achieve: when test() is called without any argument, I want that nested() function is called with default arguments.

UPD: Requirements:

  • no copy-pasting default argument value from nested() function
  • possibility to override $arr argument value
  • UPD: function test() have more than one argument (in this case, ...func_get_args() won't work)
  • actually, this is simplified problem. Imagine that there are several classes that have something like test() method with default $arr parameter that calls nested() method of other class. I want to achieve that nested() method is called with default parameters when test() method is called with default parameters.

Is that possible?

<?php

function test(int $i = 0, array $arr = null)
{
  nested($arr);
}

function nested(array $arr = [1,2,3])
{
   var_dump($arr);
}

test();
test(5, [4,5,6]);
2
  • 3
    Check if $arr is NULL, and if so, call nested(); …? Commented Dec 3, 2019 at 13:19
  • Agreed with @04FS Commented Dec 3, 2019 at 14:07

2 Answers 2

2

Instead of passing $arr into the nested function, you could instead pass in the actual parameters passed in using func_get_args() with argument unpacking (...)

function test(array $arr = null)
{
    nested(...func_get_args());
}

function nested(array $arr = [1,2,3])
{
    var_dump($arr);
}

test();

If you want it to work with a more complex argument set-up, then you can look at the number of arguments passed (func_num_args()), in this case, check if there are less than 2 arguments and if there are, then force a call without any parameters. You could check if $arr is null if there are more complex patterns, but this doesn't work if the users passes null...

function test(int $i = 0, array $arr = null)
{
    if ( func_num_args() < 2 )  {
        nested();
    }
    else    {
        nested($arr);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

That wouldn't work if test() have more than one argument
@SergiiSmirnov - There's nothing in the question about multiple arguments. If there are more states, then you need to edit your question to include them. We can't guess what you want.
@SergiiSmirnov Sure it will. More will be passed to nested but ignored.
Updated the answer.
1

Check an array is empty or not

function test(array $arr = null)
{
  if (empty($arr)) {nested();}else{nested($arr);}
}

function nested(array $arr = [1,2,3])
{
   var_dump($arr);
}

test();

4 Comments

Then what about when the $arr in test() has values and he needs that value inside nested()?
I have given condition
This won't do what they are asking for in their question though. It says "when test() is called without any argument" while you're checking if the argument is falsy. An empty array will evaluate as false as well, even though it would be an argument.
Thanks for the answer. But I need more, I've just updated the question's description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.