Pass variable number of variables to a class in PHP

Posted by user325282 on Stack Overflow See other posts from Stack Overflow or by user325282
Published on 2010-04-25T07:00:24Z Indexed on 2010/04/25 7:03 UTC
Read the original article Hit count: 155

Filed under:
|
|

I need to pass a variable number of strings to instantiate different classes. I can always do a switch on the size of the array:

switch(count($a)) {
case 1:
    new Class(${$a[0]});
    break;
case 2:
    new Class(${$a[0]}, ${$a[1]});
    break;
etc...

There has to be a better way to do this. If I have an array of strings ("variable1", "variable2", 'variable3", ...), how can I instantiate a Class without manually accounting for every possibility?

© Stack Overflow or respective owner

Related posts about php

Related posts about variable