How can I call a object constructor passing an array of parameters so that having:
$array = array($param1, $param2);
I'll be able to call
$abc = new Abc($param1, $param2);
considering that I don't know how many parameters could be set in the array.
Is there something like call_object('Abc', array($param1, $param2))
?
How about using ... (splat operator)?
$array = array($param1, $param2);
$abc = new Abc(...$array); // equal to: new Abc($param1, $param2)
PHP 5.6 is required.
The best way is to use an array or object that stores the arguments and you just pass that array/object
Another way would be using Reflection ( http://de2.php.net/Reflection ) using newInstanceArgs ( http://de2.php.net/manual/de/reflectionclass.newinstanceargs.php )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With