473,396 Members | 1,998 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,396 software developers and data experts.

Creating objects with variable parameters in contructor

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
normally.
This is why I'm looking for another solution... :p

Cheers,
zimba

<?php

function createObject($className, $parameters)
{
$c_params = count($parameters);
$c = "return new $className(";
for($i=0; $i<$c_params; $i++)
{
$c .= "\$parameters[$i]";
if ($i<$c_params-1) $c .= ',';
}
$c .= ');';
return eval($c);
}

function callMethod($object, $methodName, $parameters)
{
$c_params = count($parameters);
$c = "return \$object->$methodName(";
for($i=0; $i<$c_params; $i++)
{
$c .= "\$parameters[$i]";
if ($i<$c_params-1) $c .= ',';
}
$c .= ');';
return eval($c);
}

class MyClass
{
private $a, $b;
function __construct($a, $b)
{
$this->a = $a;
$this->b = $b;
}
function out($x, $y)
{
return $this->a .' '. $this->b . ' and '.$x .' '.$y;
}
}

$x = createObject('MyClass', array('hello', 'world'));
echo callMethod($x, 'out', array('you','also'));

?>

Jul 17 '05 #1
3 2660
zimba wrote:
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


Just use a constructor with a variable number of arguments and use
func_num_args()/func_get_args() to get the (total number of) arguments.
JW

Jul 17 '05 #2
This is not what I mean't,
the object can be arbitrary of any kind, so I don't know in advance how
much parameters it will have in the constructor.

Jul 17 '05 #3
Hi Zimba,

Take a look at http://nl.php.net/manual/en/function...func-array.php
it says:
If you need to call CLASS method (NOT object):
call_user_func_array(array('class', 'Foo'),$args);

If you program the constructors yourself, consider to give all
constuctors a single parameter, expected to be an array that holds the
actual values you would otherwise pass as parameters, that may run
faster, especially if you pass the array by reference.

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.

zimba wrote:
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
normally.
This is why I'm looking for another solution... :p

Cheers,
zimba

<?php

function createObject($className, $parameters)
{
$c_params = count($parameters);
$c = "return new $className(";
for($i=0; $i<$c_params; $i++)
{
$c .= "\$parameters[$i]";
if ($i<$c_params-1) $c .= ',';
}
$c .= ');';
return eval($c);
}

function callMethod($object, $methodName, $parameters)
{
$c_params = count($parameters);
$c = "return \$object->$methodName(";
for($i=0; $i<$c_params; $i++)
{
$c .= "\$parameters[$i]";
if ($i<$c_params-1) $c .= ',';
}
$c .= ');';
return eval($c);
}

class MyClass
{
private $a, $b;
function __construct($a, $b)
{
$this->a = $a;
$this->b = $b;
}
function out($x, $y)
{
return $this->a .' '. $this->b . ' and '.$x .' '.$y;
}
}

$x = createObject('MyClass', array('hello', 'world'));
echo callMethod($x, 'out', array('you','also'));

?>

Jul 17 '05 #4

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

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
6
by: Nick | last post by:
Id like to create some list menus dynamically. The problem is that id like the option text to be different from the option values. For example... <script language=JavaScript> city = 'Los...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
3
by: tinman | last post by:
Hi.... Assume Function A in an application calls Function GetSomeData in another assembly..... which then is the prefered method to return the SqlDatareader object back to Function A (and why...
14
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed...
0
by: Samuel Zallocco | last post by:
Hi all, I've a problem with PHP5 + PEAR::SOAP. I Have the following 2 script that implements a simple web service: The Server Code running on WinXP + PHP5 + Apache 2.x:...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.