note 91565 modified in function.call-user-func-array by danbrown
| From: | danbrown@php.net | Date: | Wed, 17 Jun 2009 12:13:12 +0000 |
| Subject: | note 91565 modified in function.call-user-func-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156971@lists.php.net to get a copy of this message | ||
If you want to use call_user_func_array to call an objects constructor, this works for me:
<?php
$obj = new $object_class();
call_user_func_array(array($obj, '__construct'), $args);
?>
(However it does depend on having a constructor where all arguments are optional, which may or may
not be possible)
So this is a two part initiation, the object gets initiated with no arguments first (hence requiring
options constructor params), and then __construct gets called again. Thus, you should not do
anything in your constructor that triggers effects outside the class... it should just set the
objects state.
--was--
If you want to use call_user_func_array to call an objects constructor, this works for me:
$obj = new $object_class();
call_user_func_array(array($obj, '__construct'), $args);
(However it does depend on having a constructor where all arguments are optional, which may or may
not be possible)
So this is a two part initiation, the object gets initiated with no arguments first (hence requiring
options constructor params), and then __construct gets called again. Thus, you should not do
anything in your constructor that triggers effects outside the class... it should just set the
objects state.
http://php.net/manual/en/function.call-user-func-array.php