473,811 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2545
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******** *************@c 13g2000cwb.goog legroups.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
2682
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 ? The following solution works, but it's around 20 times slower than
10
3529
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 { lengthSV=16 }; // Length of SomeVector vector<double> SomeVector;
5
5924
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 of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should...
2
2298
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 getting a lot of problems, but it may not be due to
8
5249
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 the constructor for example MyContorl oMyControl = new MyContorl(employeeid). However I need to load the control at runtime so the have to call it this way LoadControl("MyControl.ascx") and I get an error that I have not supplied any parameter...
4
2838
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
8393
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 arguments, but that is not recommended. If I can not find another way, I will apply that one. Regards
3
2100
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
3055
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 class method? BACKGROUND: I am developing a MySQL-specific "database manager" class and my constructor has 4 parameters: the first 3 parameters are those typically used in the mysql_connect() call which the constructor calls to make the
0
9727
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.