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'));
?> 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
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.
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'));
?> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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*...
|
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...
|
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...
|
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;...
|
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...
|
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...
|
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:...
|
by: Release |
last post by:
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |