Skip to content
Advertisement

PHP: How to spread an array as function parameters

I’m trying to use __callStatic to wrap the calls to differents static functions in my class.

This current version of the code allows me to do so with functions that have 0 or 1 parameter:

JavaScript

However it will fail for mfunc since it has two parameters. How can i spread the different arguments when i’m calling the function ?

I can’t use the new spread operator because I am locked on PHP 7.2. I can’t modify mfunc either, because I have way too much methods to change in the project.

Advertisement

Answer

The spread operator is still usable in PHP 7.2, what you point to is something slightly different (it’s about being able to use it as part of an array). So you can use

JavaScript

As a more legible version…

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement