Analog of call_user_func_array for class creation.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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?


Re: Analog of call_user_func_array for class creation.

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]
       $$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)


Henk Verhoeven,

AlexVN wrote:
Quoted text here. Click to load it

Re: Analog of call_user_func_array for class creation.


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.


Henk Verhoeven wrote:
Quoted text here. Click to load it

Re: Analog of call_user_func_array for class creation.

AlexVN wrote:
Quoted text here. Click to load it

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.


Re: Analog of call_user_func_array for class creation.

Quoted text here. Click to load it

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(){
   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 | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)  

Re: Analog of call_user_func_array for class creation.


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 =

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.


Kimmo Laine wrote:
Quoted text here. Click to load it

Site Timeline