473,246 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Variable parameters to variable class constructor

Hi

I have a PHP problem I can't solve.

I'm using variable names as class constructors in a project, a la:

$foo = "ClassName";
$bar = new $foo();

But what I can't work out is how to pass a variable number of
parameters to the constructor; call_user_func_array looks good, but I
don't think that'll work in the same way as 'new'.

So what I'm trying to do is:

$bar = new $foo($a, $b, $c);

Where I'm getting the parameters for the constructor from func_get_args
and as such, I don't know many parameters there are.
Any suggestions?

Thanks,

Justin

Jul 17 '05 #1
2 2525
co************@gmail.com wrote:
Where I'm getting the parameters for the constructor from
func_get_args and as such, I don't know many parameters there are.
Any suggestions?


http://www..php.net/manual/en/functi...c-num-args.php
JW

Jul 17 '05 #2
<co************@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
Hi

I have a PHP problem I can't solve.

I'm using variable names as class constructors in a project, a la:

$foo = "ClassName";
$bar = new $foo();

But what I can't work out is how to pass a variable number of
parameters to the constructor; call_user_func_array looks good, but I
don't think that'll work in the same way as 'new'.

So what I'm trying to do is:

$bar = new $foo($a, $b, $c);

Where I'm getting the parameters for the constructor from func_get_args
and as such, I don't know many parameters there are.
Any suggestions?

Thanks,

Justin


Time to use good old eval()

<?

$args = array("first", "second", "third");
extract($args, EXTR_PREFIX_ALL, "arg");
$names = array();
for($i = 0; $i < count($args); $i++) $names[] = "\$arg_$i";
$code = "new $class(" . implode(',', $names) . ")";
eval($code);

?>
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: zimba | last post by:
Hello, Do you know a better way to : - instanciate a class with an unknown number of parameters in the constructor - call an object's method, also with an unknown number of parameters ? ...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
2
by: John Ratliff | last post by:
I'm having issues with forward declarations and possibly member variables. Can you declare a member variable and pass it parameters. class x { private: y obj(this); } Is that valid? I'm...
8
by: Sam Kuehn | last post by:
How do I accomplish the fallowing (is it even possible). Say I write a UserControl "MyControl.ascx". Now I use LoadControl("MyControl.ascx"). But I really want MyControl to require parameters in...
4
by: ankurdave | last post by:
Is it possible to declare a class member variable in the constructor? For example, class SomeClass { public: SomeClass() { int SomeArray; } }
16
by: utab | last post by:
Dear all, How can I generate a constructor to take a variable number of arguments. Is boost tuple appropriate for this? I know that there is a way to make functions take variable number of...
3
by: John Shell | last post by:
Hello, all. The following code results in a C2666 error (2 overloads have similar conversions). class FSVec2D { public: FSVec2D() { // code omitted }
1
by: mavigozler | last post by:
Is there a strict requirement for having the same number of parameters in a call to a function and/or class method---in this case a class constructor---and the definition of the function and/or...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.