473,651 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($c lassName, $parameters)
{
$c_params = count($paramete rs);
$c = "return new $className(";
for($i=0; $i<$c_params; $i++)
{
$c .= "\$paramete rs[$i]";
if ($i<$c_params-1) $c .= ',';
}
$c .= ');';
return eval($c);
}

function callMethod($obj ect, $methodName, $parameters)
{
$c_params = count($paramete rs);
$c = "return \$object->$methodName( ";
for($i=0; $i<$c_params; $i++)
{
$c .= "\$paramete rs[$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('M yClass', array('hello', 'world'));
echo callMethod($x, 'out', array('you','al so'));

?>

Jul 17 '05 #1
3 2673
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('cl ass', '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($c lassName, $parameters)
{
$c_params = count($paramete rs);
$c = "return new $className(";
for($i=0; $i<$c_params; $i++)
{
$c .= "\$paramete rs[$i]";
if ($i<$c_params-1) $c .= ',';
}
$c .= ');';
return eval($c);
}

function callMethod($obj ect, $methodName, $parameters)
{
$c_params = count($paramete rs);
$c = "return \$object->$methodName( ";
for($i=0; $i<$c_params; $i++)
{
$c .= "\$paramete rs[$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('M yClass', array('hello', 'world'));
echo callMethod($x, 'out', array('you','al so'));

?>

Jul 17 '05 #4

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

Similar topics

23
40623
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* just created a new pointer to the original value.... For example I wanted to initialize a list of empty lists.... a=, , , , ] I thought there has to be a *really* easy way of doing it - after a
6
2563
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 "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
6
2177
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 Angeles'; city = 'New York'; city = 'San Francisco'; city = 'London'; city = 'Paris';
8
5961
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; this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_ApplicantName", dataset.Tables.Columns.DataType, 50,
3
1749
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 ?). Does the prefered option apply to all reference types ? Option 1 *******
14
1527
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 something? Regards /Niklas Public Class Main Shared Sub Main() Dim testPropObj As New MyPropertObject testPropObj.MyInt = 1
0
1909
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: ---------------------------------------------------- <?php require_once('SOAP/Server.php'); require_once('SOAP/Disco.php');
31
3180
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 some of the most powerful programming tools I have found. Often times as my program grows so do my objects. Often times I look back and see that my objects could be broken down int several smaller more re-usable module pieces of code. Is it a...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
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...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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,...
0
8579
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
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
4144
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
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1587
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.