Passing Variable Length Argument List to Parent

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

    Passing Variable Length Argument List to Parent


    hello!

    I have a class method that takes a variable number of parameters and
    correctly deals with them with func_get_args, etc ...

    i.e.

    class ABC
    {
    public function MooSaysTheCow()
    {
    foreach (func_get_args( ) as $arg_name => $arg_value)
    {
    ...
    }
    }
    }

    now, the problem is: i want to override this class and this method, have
    that overriding method call the base class and then do something else. The
    problem is that I don't know how to pass the parameters along.

    class DEF
    {
    public function MooSaysTheCow()
    {
    parent::MooSays TheCow( WHAT DO I PUT HERE ????? );
    echo "Overridden version with new exciting features!";
    }
    }


    Any ideas?

    the best i've thought of so far is terribly yucky, and not flexible to
    arbitrary numbers of parameters .... blech.

    class DEF
    {
    public function MooSaysTheCow()
    {
    switch (func_get_num_a rgs())
    {
    case 1:
    parent::MooSays TheCow(func_get _arg(0));
    break;
    case 2:
    parent::MooSays TheCow(func_get _arg(0), func_get_arg(1) );
    break;
    etc ....
    ...
    ..
  • Dani CS

    #2
    Re: Passing Variable Length Argument List to Parent

    Mark wrote:[color=blue]
    > hello!
    >
    > I have a class method that takes a variable number of parameters and
    > correctly deals with them with func_get_args, etc ...
    >
    > i.e.
    >
    > class ABC
    > {
    > public function MooSaysTheCow()
    > {
    > foreach (func_get_args( ) as $arg_name => $arg_value)
    > {
    > ...
    > }
    > }
    > }
    >
    > now, the problem is: i want to override this class and this method, have
    > that overriding method call the base class and then do something else. The
    > problem is that I don't know how to pass the parameters along.[/color]

    Check the comments for
    <http://www.php.net/manual/en/function.func-get-args.php>
    [color=blue]
    >
    > class DEF
    > {
    > public function MooSaysTheCow()
    > {
    > parent::MooSays TheCow( WHAT DO I PUT HERE ????? );
    > echo "Overridden version with new exciting features!";
    > }
    > }
    >
    >
    > Any ideas?
    >
    > the best i've thought of so far is terribly yucky, and not flexible to
    > arbitrary numbers of parameters .... blech.
    >
    > class DEF
    > {
    > public function MooSaysTheCow()
    > {
    > switch (func_get_num_a rgs())
    > {
    > case 1:
    > parent::MooSays TheCow(func_get _arg(0));
    > break;
    > case 2:
    > parent::MooSays TheCow(func_get _arg(0), func_get_arg(1) );
    > break;
    > etc ....
    > ...
    > ..
    > .
    > }
    > }
    > }
    >
    >
    > thanks.
    > mark.
    >
    >[/color]

    Comment

    • konsu

      #3
      Re: Passing Variable Length Argument List to Parent

      did you try to pass the return value of func_get_args() ?

      "Mark" <mw@ANGRYLanfea r.com> wrote in message
      news:IsednfQ8IZ ZKrSvcRVn-rQ@nventure.com ...[color=blue]
      >
      > hello!
      >
      > I have a class method that takes a variable number of parameters and
      > correctly deals with them with func_get_args, etc ...
      >
      > i.e.
      >
      > class ABC
      > {
      > public function MooSaysTheCow()
      > {
      > foreach (func_get_args( ) as $arg_name => $arg_value)
      > {
      > ...
      > }
      > }
      > }
      >
      > now, the problem is: i want to override this class and this method, have
      > that overriding method call the base class and then do something else.
      > The
      > problem is that I don't know how to pass the parameters along.
      >
      > class DEF
      > {
      > public function MooSaysTheCow()
      > {
      > parent::MooSays TheCow( WHAT DO I PUT HERE ????? );
      > echo "Overridden version with new exciting features!";
      > }
      > }
      >
      >
      > Any ideas?
      >
      > the best i've thought of so far is terribly yucky, and not flexible to
      > arbitrary numbers of parameters .... blech.
      >
      > class DEF
      > {
      > public function MooSaysTheCow()
      > {
      > switch (func_get_num_a rgs())
      > {
      > case 1:
      > parent::MooSays TheCow(func_get _arg(0));
      > break;
      > case 2:
      > parent::MooSays TheCow(func_get _arg(0), func_get_arg(1) );
      > break;
      > etc ....
      > ...
      > ..
      > .
      > }
      > }
      > }
      >
      >
      > thanks.
      > mark.
      >
      >
      > --
      > I am not an ANGRY man. Remove the rage from my email to reply.
      >
      > Ashlee Simpson Sings! You Faint .... --More--
      > Ashlee Simpson Sings! Ashlee Simpsons sings! You die ... --More--[/color]


      Comment

      • Andy Hassall

        #4
        Re: Passing Variable Length Argument List to Parent

        On Tue, 07 Dec 2004 15:37:52 -0800, Mark <mw@ANGRYLanfea r.com> wrote:
        [color=blue]
        > I have a class method that takes a variable number of parameters and
        >correctly deals with them with func_get_args, etc ...
        >
        > i.e.
        >
        > class ABC
        > {
        > public function MooSaysTheCow()
        > {
        > foreach (func_get_args( ) as $arg_name => $arg_value)
        > {
        > ...
        > }
        > }
        > }
        >
        > now, the problem is: i want to override this class and this method, have
        >that overriding method call the base class and then do something else. The
        >problem is that I don't know how to pass the parameters along.
        >
        > class DEF
        > {
        > public function MooSaysTheCow()
        > {
        > parent::MooSays TheCow( WHAT DO I PUT HERE ????? );
        > echo "Overridden version with new exciting features!";
        > }
        > }
        >
        >
        > Any ideas?
        >
        > the best i've thought of so far is terribly yucky, and not flexible to
        >arbitrary numbers of parameters .... blech.
        >
        > class DEF
        > {
        > public function MooSaysTheCow()
        > {
        > switch (func_get_num_a rgs())
        > {
        > case 1:
        > parent::MooSays TheCow(func_get _arg(0));
        > break;
        > case 2:
        > parent::MooSays TheCow(func_get _arg(0), func_get_arg(1) );
        > break;
        > etc ....
        > ...
        > ..
        > .
        > }
        > }
        > }[/color]

        What am I missing here; can't you just use func_get_args() to pass the
        arguments upwards?

        --
        Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
        <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

        Comment

        • Mark

          #5
          Re: Passing Variable Length Argument List to Parent

          Dani CS wrote:
          [color=blue]
          >
          > Check the comments for
          > <http://www.php.net/manual/en/function.func-get-args.php>[/color]

          Close!

          func_get_args() doesn't work because the parent function has to then be
          expecting an array parameter, not a list of parameters as normal (and i do
          NOT have control over the parent implementation -- it's a prepackaged
          class).

          i was moderately hopeful when I saw the function call_user_func_ array,
          which does what i want -- takes an array with parameters and calls the
          function with the array values as normal arguments

          EXCEPT -- that won't work within classes, expeshully in the __construct
          method ...

          argh.

          any other ideas?


          thanks!
          mark.

          Comment

          • Joshua Beall

            #6
            Re: Passing Variable Length Argument List to Parent

            I'm typing this without testing it, but it seems like it would work. I'm
            sure you take a performance hit from the eval() call, though.

            $argList = '';
            for($n=0;$n<fun c_get_num_args( );$n++)
            $argList .= "func_get_arg($ n),";
            $argList = rtrim($argList, ',');
            /* $argList will not be a string that looks like
            "func_get_art(0 ),func_get_arg( 1),func_get_arg (2)", repeating for however
            many arguments have been passed*/
            eval("parent::M ooSaysTheCow($a rgList);");

            Worth a shot.


            Comment

            • Michael Fesser

              #7
              Re: Passing Variable Length Argument List to Parent

              .oO(Mark)
              [color=blue]
              > I have a class method that takes a variable number of parameters and
              >correctly deals with them with func_get_args, etc ... [...]
              >
              > now, the problem is: i want to override this class and this method, have
              >that overriding method call the base class and then do something else. The
              >problem is that I don't know how to pass the parameters along.
              >
              > class DEF
              > {
              > public function MooSaysTheCow()
              > {
              > parent::MooSays TheCow( WHAT DO I PUT HERE ????? );
              > echo "Overridden version with new exciting features!";
              > }
              > }[/color]

              Try

              public function MooSaysTheCow() {
              $args = func_get_args() ;
              call_user_func_ array(array('pa rent', 'MooSaysTheCow' ), $args);
              }

              Micha

              Comment

              • Joshua Beall

                #8
                Re: Passing Variable Length Argument List to Parent

                "Michael Fesser" <netizen@gmx.ne t> wrote in message
                news:hf5er09fhd skuf07ap47s3frc chs7cg8ad@4ax.c om...[color=blue]
                > Try
                >
                > public function MooSaysTheCow() {
                > $args = func_get_args() ;
                > call_user_func_ array(array('pa rent', 'MooSaysTheCow' ), $args);
                > }[/color]

                Very clever. Mark, please report back and let us know if this works for
                you!


                Comment

                • Mark

                  #9
                  Re: Passing Variable Length Argument List to Parent

                  Joshua Beall wrote:
                  [color=blue]
                  > "Michael Fesser" <netizen@gmx.ne t> wrote in message
                  > news:hf5er09fhd skuf07ap47s3frc chs7cg8ad@4ax.c om...[color=green]
                  >> Try
                  >>
                  >> public function MooSaysTheCow() {
                  >> $args = func_get_args() ;
                  >> call_user_func_ array(array('pa rent', 'MooSaysTheCow' ), $args);
                  >> }[/color]
                  >
                  > Very clever. Mark, please report back and let us know if this works for
                  > you![/color]

                  nope. probably would have in PHP4, but most certainly not in PHP5.

                  i'm still stuck with the switch statement in PHP5, unfortunately.

                  argh!
                  mark.


                  --
                  I am not an ANGRY man. Remove the rage from my email to reply.

                  Comment

                  • Mark

                    #10
                    Re: Passing Variable Length Argument List to Parent

                    Andy Hassall wrote:
                    [color=blue]
                    > On Tue, 07 Dec 2004 15:37:52 -0800, Mark <mw@ANGRYLanfea r.com> wrote:
                    >[/color]
                    [color=blue]
                    >
                    > What am I missing here; can't you just use func_get_args() to pass the
                    > arguments upwards?[/color]

                    no. at least not in overloaded methods in classes.

                    the problem is that the overloadER and the overloadEE are both looking for
                    variable argument lists. func_get_args() returns an ARRAY.

                    now, the obvious (lame) solution would be to modify both functions to just
                    take arrays of arguments but ... i don't have control over the base class's
                    implementation (it's the mysqli class).

                    so, i appear to be stuck with the switch statement way of doing things.
                    d'oh!

                    thanks,
                    mark.



                    --
                    I am not an ANGRY man. Remove the rage from my email to reply.

                    Comment

                    • Michael Fesser

                      #11
                      Re: Passing Variable Length Argument List to Parent

                      .oO(Mark)
                      [color=blue]
                      > nope. probably would have in PHP4, but most certainly not in PHP5.[/color]

                      Hmm. The following works here on PHP5 when called statically:

                      class TBar {
                      function doSomething() {
                      print_r(func_ge t_args());
                      }
                      }

                      class TFoo extends TBar {
                      function doSomething() {
                      $args = func_get_args() ;
                      call_user_func_ array(array('pa rent', 'doSomething'), $args);
                      }
                      }

                      TFoo::doSomethi ng(23, 42);

                      If called on an instance of TFoo my PHP crashes. But if I declare the
                      above methods 'static' then it works, even if invoked non-statically ...
                      [color=blue]
                      > i'm still stuck with the switch statement in PHP5, unfortunately.[/color]

                      You could also try it with eval(), as already mentioned in another post.

                      Micha

                      Comment

                      • Mark

                        #12
                        Re: Passing Variable Length Argument List to Parent

                        Michael Fesser wrote:

                        [color=blue]
                        > Hmm. The following works here on PHP5 when called statically:
                        >
                        > class TBar {
                        > function doSomething() {
                        > print_r(func_ge t_args());
                        > }
                        > }
                        >
                        > class TFoo extends TBar {
                        > function doSomething() {
                        > $args = func_get_args() ;
                        > call_user_func_ array(array('pa rent', 'doSomething'), $args);
                        > }
                        > }
                        >
                        > TFoo::doSomethi ng(23, 42);[/color]

                        aaaah. the problem is that i am trying to do this in the constructor. of
                        the mysqli class.

                        argh.

                        such is life :-).

                        [color=blue]
                        > You could also try it with eval(), as already mentioned in another post.[/color]

                        !aaaieee!

                        might be the only solution.

                        thanks much!
                        [color=blue]
                        > Micha[/color]

                        --
                        I am not an ANGRY man. Remove the rage from my email to reply.

                        Comment

                        Working...