By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Analog of call_user_func_array for class creation.

P: n/a
Hi,

I would like to know if someone investigated the method of creating
classes dynamically depending on the name. I do not like a lot of ifs
and I suppose that something like call_user_func_array('ClassName',
array($param1, $param2, $param3, ..., $paramN)) should exist over the
PHP language. Any suggestions?

Thanks,
Alexander
http://www.alexatnet.com/

Jun 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Alexander,

Your message seems somewhat mixed-up. As far as i know the only way to
create a class is to have php interpret a class definition like:

class MyClass extends MySuperclass {
//variables and methods go here
}

As far as i know the following does not work:

$className = 'MyClass';
$superclassName = 'MySuperclass';
class $className extends $superclassName {
//variables and methods go here
}
(please correct me if i am wrong - if this actually would work!)

Furthermore you can call static methods as well as instance methods
using call_user_func_array according to the explanation of the the
callback type:
- Static class methods can also be passed without instantiating an
object of that class by passing the class name instead of an object as
the element with index 0.
- A method of an instantiated object is passed as an array containing an
object as the element with index 0 and a method name as the element with
index 1.

Finally you can do is INSTANTIATE a class dynamicly:
$myClassName = 'MyClass';
$newObject = new $myClassName($param1, $param2);
I agree this does nog allow you to pass the parameters as an array.
Is that what you want?

You ARE allowed to pass more parameters then the constructor of the
class needs. You could write a function like this that will pass a
limited number of parameters from an array:

function instantiate_array($className, $parameters) {
if (count($parameters) > 4)
trigger_error('too many parameters', E_USER_ERROR);
for ($i=0; $i<4; $i++) {
$varName = "p$i";
if (isSet($parameters[$i]);
$$varName = $parameters[$i]
else
$$varName = null;
}
return new $className($p0, $p1, $p2, $p3);
}

I agree this does have an if statement, but at least if you need more
parameters, you do not need to add more if statments.
(Possible disadvantage: the null values that are passed may override
defaults defined in the constructors parameter list)

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.
AlexVN wrote:
Hi,

I would like to know if someone investigated the method of creating
classes dynamically depending on the name. I do not like a lot of ifs
and I suppose that something like call_user_func_array('ClassName',
array($param1, $param2, $param3, ..., $paramN)) should exist over the
PHP language. Any suggestions?

Thanks,
Alexander
http://www.alexatnet.com/

Jun 15 '06 #2

P: n/a
Henk,

Thank you for the great answer. You are right--I'm looking for a method
of instantiating class with dynamic parameters, not creating. I
understant the method you proposed, but what I would much rather see is
a method of calling class constructor with dynamic number of
parameters. I suspect that PHP does not have such method (since you, a
guru, do not listed it here) and will try to create a couple of ifs for
my case.

Thanks,
Alexander
http://www.alexatnet.com/

Henk Verhoeven wrote:
Hi Alexander,

Your message seems somewhat mixed-up. As far as i know the only way to
create a class is to have php interpret a class definition like:

class MyClass extends MySuperclass {
//variables and methods go here
}

As far as i know the following does not work:

$className = 'MyClass';
$superclassName = 'MySuperclass';
class $className extends $superclassName {
//variables and methods go here
}
(please correct me if i am wrong - if this actually would work!)

Furthermore you can call static methods as well as instance methods
using call_user_func_array according to the explanation of the the
callback type:
- Static class methods can also be passed without instantiating an
object of that class by passing the class name instead of an object as
the element with index 0.
- A method of an instantiated object is passed as an array containing an
object as the element with index 0 and a method name as the element with
index 1.

Finally you can do is INSTANTIATE a class dynamicly:
$myClassName = 'MyClass';
$newObject = new $myClassName($param1, $param2);
I agree this does nog allow you to pass the parameters as an array.
Is that what you want?

You ARE allowed to pass more parameters then the constructor of the
class needs. You could write a function like this that will pass a
limited number of parameters from an array:

function instantiate_array($className, $parameters) {
if (count($parameters) > 4)
trigger_error('too many parameters', E_USER_ERROR);
for ($i=0; $i<4; $i++) {
$varName = "p$i";
if (isSet($parameters[$i]);
$$varName = $parameters[$i]
else
$$varName = null;
}
return new $className($p0, $p1, $p2, $p3);
}

I agree this does have an if statement, but at least if you need more
parameters, you do not need to add more if statments.
(Possible disadvantage: the null values that are passed may override
defaults defined in the constructors parameter list)

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.
AlexVN wrote:
Hi,

I would like to know if someone investigated the method of creating
classes dynamically depending on the name. I do not like a lot of ifs
and I suppose that something like call_user_func_array('ClassName',
array($param1, $param2, $param3, ..., $paramN)) should exist over the
PHP language. Any suggestions?

Thanks,
Alexander
http://www.alexatnet.com/


Jun 15 '06 #3

P: n/a
AlexVN wrote:
Henk,

Thank you for the great answer. You are right--I'm looking for a method
of instantiating class with dynamic parameters, not creating. I
understant the method you proposed, but what I would much rather see is
a method of calling class constructor with dynamic number of
parameters. I suspect that PHP does not have such method (since you, a
guru, do not listed it here) and will try to create a couple of ifs for
my case.

Thanks,
Alexander
http://www.alexatnet.com/


I know its not quite what you had in mind (i.e. polymorphism) but I have
had some success with passing an associative array to a class
constructor and then letting the constructor figure out what to do. This
is a bit different from the $foo = null type of setup since you may
supply any number of keys in the associative array but undefined keys
have no value.

-david-

Jun 15 '06 #4

P: n/a
"AlexVN" <al*****************@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Henk,

Thank you for the great answer. You are right--I'm looking for a method
of instantiating class with dynamic parameters, not creating. I
understant the method you proposed, but what I would much rather see is
a method of calling class constructor with dynamic number of
parameters. I suspect that PHP does not have such method (since you, a
guru, do not listed it here) and will try to create a couple of ifs for
my case.


If I understood correctly what you mean, it is possible. I did something
like this once. I wanted to instantiate an object of a class which had an
arbitary number of columns. It was sort of a table container. All columns
had a certain length and I create the structure of the table in the
constructor... So to create a table with two columns, one 20 units wide and
another 40 units wide, I'd call the constructor $myTable = new table(20,40);
or just as well new table(100,20,30,5,12,40);
Here's the definition of the constructor:

public function __construct(){
if(func_num_args()){
foreach(func_get_args() as $arg){
$this->columns[] = $arg;
}
}
}

So as you see, the constructor has no parameters at all, I just catch all
that were passed to it with the func_get_args() function that returns a list
of all the parameters passed to the method, and walk it thru. All very
simple. Is this what you mean?

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Jun 16 '06 #5

P: n/a
Kimmo,

Thank for for the answer, but what I mean is a little bit different. I
want to simplify the following construction:

function CreateClass($className) {
switch (func_num_args()) {
case 1:
return new $className();
case 2:
return new $className(func_get_arg(1));
case 3:
return new $className(func_get_arg(1), func_get_arg(2));
...
}
}

In other words, I want something like:
function CreateClass($className) {
return create_user_class_array($className, array_shift($a =
func_get_args()));
}

Where create_user_class_array is the "magic" method similar to
call_user_func_array, which, I hope, should exist and I just did not
find it... But now I almost sure that there is no such method.

Thanks,
Alexander
http://www.alexatnet.com/

Kimmo Laine wrote:
"AlexVN" <al*****************@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Henk,

Thank you for the great answer. You are right--I'm looking for a method
of instantiating class with dynamic parameters, not creating. I
understant the method you proposed, but what I would much rather see is
a method of calling class constructor with dynamic number of
parameters. I suspect that PHP does not have such method (since you, a
guru, do not listed it here) and will try to create a couple of ifs for
my case.


If I understood correctly what you mean, it is possible. I did something
like this once. I wanted to instantiate an object of a class which had an
arbitary number of columns. It was sort of a table container. All columns
had a certain length and I create the structure of the table in the
constructor... So to create a table with two columns, one 20 units wide and
another 40 units wide, I'd call the constructor $myTable = new table(20,40);
or just as well new table(100,20,30,5,12,40);
Here's the definition of the constructor:

public function __construct(){
if(func_num_args()){
foreach(func_get_args() as $arg){
$this->columns[] = $arg;
}
}
}

So as you see, the constructor has no parameters at all, I just catch all
that were passed to it with the func_get_args() function that returns a list
of all the parameters passed to the method, and walk it thru. All very
simple. Is this what you mean?

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)


Jun 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.