0

I have a function as follows:

/**
 * @param \string[] ...$whitelist
 * @return Array
 */
public function whitelist(string ...$whitelist): Array
{
    // code
}

How can I pass an array (e.g $arr = [$val1, $val2, ...]) to the function?

Note that I cannot change the function structure because it belongs to a third party class I am using in my code.

2
  • Thanks for the note. :) Commented Sep 26, 2018 at 14:13
  • Hey Hamid, I edited my answer, please check it out. :) Commented Sep 26, 2018 at 16:10

1 Answer 1

1
/**
 * @param \string[] ...$whitelist
 * @return Array
 */
public function whitelist(string ...$whitelist): Array
{
    // code
}

You pass the arguments by separating them by comma, meaning the whitelist function takes n number of arguments. You are using the splat operator (https://lornajane.net/posts/2014/php-5-6-and-the-splat-operator). So, in your case it may be something like:

whitelist(...['Item 1', 'Item 2', 'Item 3']);
Sign up to request clarification or add additional context in comments.

Comments

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.