11

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))?

3
  • 4
    why not just pass the array ? new Abc($array) ? Commented Dec 16, 2011 at 14:16
  • 3
    Maybe Jeff doesn't write/control those classes... Commented Dec 16, 2011 at 14:21
  • @JeffPigarelli perhaps if you had worded your question better or given more of an example you would not have got so many "incorrect" answers .... Commented Dec 16, 2011 at 14:27

4 Answers 4

21

How about using ... (splat operator)?

$array = array($param1, $param2);
$abc = new Abc(...$array); // equal to: new Abc($param1, $param2)

PHP 5.6 is required.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually this should be marked as the right answer, because PHP 5.6 was released 5 years ago and isn't supported anymore. So users are encouraged to use the latest stable PHP version anyway.
7

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 ( https://www.php.net/Reflection ) using newInstanceArgs ( https://www.php.net/manual/de/reflectionclass.newinstanceargs.php )

Comments

4

The ideal is to define your constructor to take an array.

If you can't do that, there is a possible workaround. If all parameters to the constructor are optional, you could do something like this with call_user_func_array:

$obj = new Abc;
call_user_func_array(array($obj, '__construct'), $array);

This results in your constructor being run twice: once with no parameters, and once with those in the array. You'll have to decide whether this is suitable for your application.

2 Comments

Uhm! What if your Abc constructor need parameters? PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Abc::__construct(), 0 passed. So you cannot instanziate the object and then call the constructor. I'm missing something?
@guido "If all the parameters to the constructor are optional." This is hardly good practice: it's a workaround.
3

Assuming you are able to modify your objects' constructors, a pattern like this isn't uncommon, but requires associative arrays as input:

class Abc {
  public $prop1;
  public $prop2;

  public function __construct($params) {
    if (is_array($params)) {
       $this->prop1 = isset($params['prop1']) ? $params['prop1'] : NULL;
       $this->prop2 = isset($params['prop2']) ? $params['prop2'] : NULL;
    }
  }
}

// Created as:
$params = array('prop1'=>12354, 'prop2'=>54321);
$abc = new Abc($params);

1 Comment

This won't work if you want to use a typed constructor.

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.