0

I'm trying to create an object and then call a method with custom parameters.

I can't use call_user_func_array because this doesn't create a new instance of the object, which I need later for calling $this in my classes.

$params = array('param1', 'param2', 'etc') ;

How can I do this so the result is equivelant to:

$controller = new $class ;
$controller->$method($param1, $param2, $etc) ;

I also don't want to use ReflectionClass as it is reported to be slower than instantiating.

1 Answer 1

2

Yes you can use call_user_func_array:

$controller = new $class;
call_user_func_array(array($controller, $method), $params);

ref: http://php.net/manual/function.call-user-func-array.php

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

2 Comments

Thanks, I didn't I could use it like that :).
@Laz Karimov 9 minutes remain :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.