Jump to content

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...