views:

69

answers:

1

Is there a function I can use to instantiate an object with arguments?

#include <database.h>
class database
{
    function __construct($dbhost, $user, $pass, $etc) { /* etc */ }
    function query($sql) { /* dowork*/ }
}
$args = array('localhost', 'user', 'pass', 'etc');

$db = create_object('database', $args); // is there a function like this?
$db->query('SELECT * FROM poop');
+1  A: 

You can use ReflectionClass::newInstanceArgs for this:

class database
{
    function __construct($dbhost, $user, $pass, $etc) { /* etc */ }
    function query($sql) { /* dowork*/ }
}
$args = array('localhost', 'user', 'pass', 'etc');

$ref = new ReflectionClass('database');
$db = $ref->newInstanceArgs($args); 
Lukáš Lalinský
yes that good to use reflection class, but it fails if arguments are passed by referecne.
Sarfraz
What exactly would fail?
Lukáš Lalinský
@Sarfraz: It wouldn't fail. In the example above, he is passing by value anyway... But if you want to pass by reference using the example above, you just need to make sure that a reference of your variable is inserted into `$args` instead of the variable itself. (`$args = array($byValue, `)
Andrew Moore
And if you have to automate this you can inspect the parameters of the constructor with ReflectionParameter::isPassedByReference()
VolkerK
@VolkerK: that's what i was expecting as an answer isPassedByReferece is the way to go :)
Sarfraz
how it would fail can be found here: http://www.php.net/manual/en/reflectionclass.newinstanceargs.php. There i have provided a possible solution on how to avoid this problem, have a loot answer by sarfraznawaz2005
Sarfraz
@Sarfraz: Do you even know what `isPassedByReference()` does and returns? It doesn't answer the question at all... Its not *"the way to go"*, it is simply a tool to determine if a particular parameter needs to be a reference or not. It is not a way to pass arguments by reference to a function or class constructor. Also, using your argument, the above code could equally fail if the number of arguments isn't the same, if the arguments are not in the same order, etc... The question is about targeting a known constructor signature, not anticipating all possible signatures.
Andrew Moore
@Sarfraz: At the risk of repeating myself, the question is about targeting a known constructor signature, not anticipating all possible signatures. Of course it will fail if an argument needs to be passed by reference. Of course it will fail if the number of arguments isn't the same. Of course it will fail if the order of arguments isn't the same. But it's outside the scope of this question. And the solution to all these problems isn't to use your overly complicated function you posted in the docs, but to simply define your `$args` array according to the target constructor signature.
Andrew Moore
@Andrew: I do know that isPassedByReference() checks whether an arguments is passed by referecen of not. Thanks YOU ARE RIGHT AS ALWAYS AND WILL REMAIN TO BE SO, AND THANKS FOR CORRECTING ME, WON'T BE RESPONDING ANYMORE. THANKS AGAIN FOR "CORRECTING" ME, HAVE A GOOD DAY AHEAD :)
Sarfraz
@Andrew: I never said that Lukáš Lalinský has posted a wrong answer, i just added more info to his answer, i know now you would say that i should have had better wording for that, sorry i did not realize that.
Sarfraz
@Sarfraz: No need to show hate. I meant no disrespect. Oh, and you might want to update that blog post of yours.... My greasemonkey script also works in Chrome ;)
Andrew Moore
@Andrew: i did not mean hate, thanks for that chrome info and by the way i have updated article so that it not make anyone feel sort of abusing SO system.
Sarfraz
and btw: a constructor with a reference parameter... perfectly valid and all of you can probably cite dozens of useful examples by heart ...yet it would most certainly catch my attention, in php anyway ;-)
VolkerK