ckrudelux Posted September 9, 2011 Share Posted September 9, 2011 I just find my self thinking about how php reads arguments in a call, reason for this is what I just got stuck in call_user_func_array(). I'm calling the construct method in one class from this function with some arguments. The problem I face now is that I call the construct method 2 times which is not wanted. $params = array('arg1', 'arg2', 'arg3'); <?phpcall_user_func_array(array(new $_GET['class'], '__construct'), $params);?> This would be great if it worked like the one above but this just sends an array as argument. <?phpnew $_GET['class']($params);?> Any suggestions how to solve this? Link to comment Share on other sites More sharing options...
JamesB Posted September 10, 2011 Share Posted September 10, 2011 you could always use a method to take the var args,or even worse, have an "if (!$param1) return" check in the constructor. i'm not sure how to do it properly though, but i did find these:http://stackoverflow...uments-with-phphttp://www.php.net/m...nstanceargs.php Link to comment Share on other sites More sharing options...
boen_robot Posted September 10, 2011 Share Posted September 10, 2011 I believe you can surround the classname with {}, and it would work, i.e. new {$_GET['class']}($params); Assuming that the class' constructor takes one array argument.If the class instead accepts multiple arguments, you can instead do: call_user_func_array(array($_GET['class'], '__construct'), $params); (note the missing "new")When you use "new", you're creating a new object. Callbacks in general don't have to be specified with an object. They can also be specified with a string that is a class to instantiate. Link to comment Share on other sites More sharing options...
ckrudelux Posted September 10, 2011 Author Share Posted September 10, 2011 I believe you can surround the classname with {}, and it would work, i.e.new {$_GET['class']}($params); Assuming that the class' constructor takes one array argument. If the class instead accepts multiple arguments, you can instead do: call_user_func_array(array($_GET['class'], '__construct'), $params); (note the missing "new") When you use "new", you're creating a new object. Callbacks in general don't have to be specified with an object. They can also be specified with a string that is a class to instantiate. Removing "new" results in this error: Fatal error: Non-static method test::__construct() cannot be called statically in /var/www/demo/ec/ec.php on line 157 Yes it's multiple arguments and I don't want to force every single class to only accept one array would be perfect if I could do a none static call with call_user_func_array() you could always use a method to take the var args,or even worse, have an "if (!$param1) return" check in the constructor. No I can not use a if statement in the constructor of the calling class cause it isn't allways my writings the best I can do is forcing arrays but that aint pretty or get dubble call. The ReflectionClass call seems like a solution.. http://www.php.net/manual/en/reflectionclass.newinstanceargs.php Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.