Analog of call_user_func_array for class creation.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AlexVN

    Analog of call_user_func_array for class creation.

    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('ClassNam e',
    array($param1, $param2, $param3, ..., $paramN)) should exist over the
    PHP language. Any suggestions?

    Thanks,
    Alexander
    Jangan lewatkan kesempatan menang di COCOL88, situs slot paling viral 2026. Bonus New Member melimpah, event petir tiap jam, dan winrate hingga 98%. Cek RTP COCOL88 sekarang!


  • Henk Verhoeven

    #2
    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($p aram1, $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_arr ay($className, $parameters) {
    if (count($paramet ers) > 4)
    trigger_error(' too many parameters', E_USER_ERROR);
    for ($i=0; $i<4; $i++) {
    $varName = "p$i";
    if (isSet($paramet ers[$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:[color=blue]
    > 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('ClassNam e',
    > array($param1, $param2, $param3, ..., $paramN)) should exist over the
    > PHP language. Any suggestions?
    >
    > Thanks,
    > Alexander
    > http://www.alexatnet.com/
    >[/color]

    Comment

    • AlexVN

      #3
      Re: Analog of call_user_func_ array for class creation.

      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
      Jangan lewatkan kesempatan menang di COCOL88, situs slot paling viral 2026. Bonus New Member melimpah, event petir tiap jam, dan winrate hingga 98%. Cek RTP COCOL88 sekarang!


      Henk Verhoeven wrote:[color=blue]
      > 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($p aram1, $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_arr ay($className, $parameters) {
      > if (count($paramet ers) > 4)
      > trigger_error(' too many parameters', E_USER_ERROR);
      > for ($i=0; $i<4; $i++) {
      > $varName = "p$i";
      > if (isSet($paramet ers[$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:[color=green]
      > > 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('ClassNam e',
      > > array($param1, $param2, $param3, ..., $paramN)) should exist over the
      > > PHP language. Any suggestions?
      > >
      > > Thanks,
      > > Alexander
      > > http://www.alexatnet.com/
      > >[/color][/color]

      Comment

      • David Haynes

        #4
        Re: Analog of call_user_func_ array for class creation.

        AlexVN wrote:[color=blue]
        > 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/[/color]

        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-

        Comment

        • Kimmo Laine

          #5
          Re: Analog of call_user_func_ array for class creation.

          "AlexVN" <alexander.netk achev@gmail.com > wrote in message
          news:1150392090 .453141.112440@ u72g2000cwu.goo glegroups.com.. .[color=blue]
          > 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.[/color]

          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_arg s()){
          foreach(func_ge t_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
          spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


          Comment

          • AlexVN

            #6
            Re: Analog of call_user_func_ array for class creation.

            Kimmo,

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

            function CreateClass($cl assName) {
            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($cl assName) {
            return create_user_cla ss_array($class Name, array_shift($a =
            func_get_args() ));
            }

            Where create_user_cla ss_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
            Jangan lewatkan kesempatan menang di COCOL88, situs slot paling viral 2026. Bonus New Member melimpah, event petir tiap jam, dan winrate hingga 98%. Cek RTP COCOL88 sekarang!


            Kimmo Laine wrote:[color=blue]
            > "AlexVN" <alexander.netk achev@gmail.com > wrote in message
            > news:1150392090 .453141.112440@ u72g2000cwu.goo glegroups.com.. .[color=green]
            > > 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.[/color]
            >
            > 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_arg s()){
            > foreach(func_ge t_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
            > spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)[/color]

            Comment

            Working...