$result = $className::$methodName(); - possible?

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

    #1

    $result = $className::$methodName(); - possible?

    Hi All,

    I have been trying to dynamically call a static member function, as follows:

    $className = 'MyClass';
    $methodName = 'MyMethod'
    $result = $className::$me thodName();

    However, I get a parse error when I do this:

    Parse error: parse error, unexpected T_PAAMAYIM_NEKU DOTAYIM in
    /home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrati onBridgerClasse s.inc
    on line 28

    Line 28 is indeed the line in which $result = $className::$me thodName();
    appears.

    Is there any way to do what I am trying to do? I have had success
    dynamically calling methods on known classes. E.g.,

    $methodName = 'MyMethod';
    $result = MyClass::$metho dName();

    This works fine. What if I want to have the class name dynamically defined
    as well? Is there any way to do that?

    Thanks for any pointers!


  • Michael Fesser

    #2
    Re: $result = $className::$me thodName(); - possible?

    .oO(Joshua Beall)
    [color=blue]
    >Is there any way to do what I am trying to do? I have had success
    >dynamically calling methods on known classes. E.g.,
    >
    >$methodName = 'MyMethod';
    >$result = MyClass::$metho dName();
    >
    >This works fine. What if I want to have the class name dynamically defined
    >as well? Is there any way to do that?[/color]

    call_user_func( ) should help.

    Micha

    Comment

    • Jay Holyer-Riviere

      #3
      Re: $result = $className::$me thodName(); - possible?

      Joshua Beall wrote:[color=blue]
      > Hi All,
      >
      > I have been trying to dynamically call a static member function, as follows:
      >
      > $className = 'MyClass';
      > $methodName = 'MyMethod'
      > $result = $className::$me thodName();
      >
      > However, I get a parse error when I do this:
      >
      > Parse error: parse error, unexpected T_PAAMAYIM_NEKU DOTAYIM in
      > /home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrati onBridgerClasse s.inc
      > on line 28
      >
      > Line 28 is indeed the line in which $result = $className::$me thodName();
      > appears.
      >
      > Is there any way to do what I am trying to do? I have had success
      > dynamically calling methods on known classes. E.g.,
      >
      > $methodName = 'MyMethod';
      > $result = MyClass::$metho dName();
      >
      > This works fine. What if I want to have the class name dynamically defined
      > as well? Is there any way to do that?
      >
      > Thanks for any pointers!
      >
      >[/color]

      In your code is the semicolon(;) missing or is that a typo?
      $className = 'MyClass';
      $methodName = 'MyMethod'; <---- semicolon
      $result = $className::$me thodName();

      Comment

      • Joshua Beall

        #4
        Re: $result = $className::$me thodName(); - possible?

        "Jay Holyer-Riviere" <jay@loudmouthe d.net> wrote in message
        news:10v0namrvc gced9@corp.supe rnews.com...[color=blue]
        > In your code is the semicolon(;) missing or is that a typo?
        > $className = 'MyClass';
        > $methodName = 'MyMethod'; <---- semicolon
        > $result = $className::$me thodName();[/color]

        Oops, typo - sorry about that.

        I simplified my code for the sake of example, and I missed that semicolon.


        Comment

        • Joshua Beall

          #5
          Re: $result = $className::$me thodName(); - possible?

          "Michael Fesser" <netizen@gmx.ne t> wrote in message
          news:d1l0v0lfbm 4b4bdlji3p5j1mk l69p0a4pu@4ax.c om...[color=blue]
          > .oO(Joshua Beall)
          >[color=green]
          >>Is there any way to do what I am trying to do? I have had success
          >>dynamically calling methods on known classes. E.g.,
          >>
          >>$methodName = 'MyMethod';
          >>$result = MyClass::$metho dName();
          >>
          >>This works fine. What if I want to have the class name dynamically
          >>defined
          >>as well? Is there any way to do that?[/color]
          >
          > call_user_func( ) should help.[/color]

          call_user_func( array($classNam e,$methodName), $parameter); did the trick.
          Thanks for the pointer, Michael.


          Comment

          • Janwillem Borleffs

            #6
            Re: $result = $className::$me thodName(); - possible?

            Joshua Beall wrote:[color=blue]
            > Is there any way to do what I am trying to do? I have had success
            > dynamically calling methods on known classes. E.g.,
            >
            > $methodName = 'MyMethod';
            > $result = MyClass::$metho dName();
            >
            > This works fine. What if I want to have the class name dynamically
            > defined as well? Is there any way to do that?
            >[/color]

            $className = 'MyClass';
            $methodName = 'MyMethod'
            $result = call_user_func( array($classNam e, $methodName));


            JW



            Comment

            • Terje Slettebø

              #7
              Re: $result = $className::$me thodName(); - possible?

              "Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> wrote in message
              news:HWXHd.1072 $BL3.484@trnddc 01...[color=blue]
              > Hi All,
              >
              > I have been trying to dynamically call a static member function, as[/color]
              follows:[color=blue]
              >
              > $className = 'MyClass';
              > $methodName = 'MyMethod'
              > $result = $className::$me thodName();
              >
              > However, I get a parse error when I do this:
              >
              > Parse error: parse error, unexpected T_PAAMAYIM_NEKU DOTAYIM in
              >[/color]
              /home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrati onBridgerClasse s.in
              c[color=blue]
              > on line 28[/color]

              There's a workaround, at least:

              $result = call_user_func( array($classNam e,$methodName), <any parameters
              here>);

              Regards,

              Terje


              Comment

              • Ashmodai

                #8
                Re: $result = $className::$me thodName(); - possible?

                Joshua Beall scribbled something along the lines of:[color=blue]
                > Hi All,
                >
                > I have been trying to dynamically call a static member function, as follows:
                >
                > $className = 'MyClass';
                > $methodName = 'MyMethod'
                > $result = $className::$me thodName();
                >
                > However, I get a parse error when I do this:
                >
                > Parse error: parse error, unexpected T_PAAMAYIM_NEKU DOTAYIM in
                > /home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrati onBridgerClasse s.inc
                > on line 28
                >
                > Line 28 is indeed the line in which $result = $className::$me thodName();
                > appears.
                >
                > Is there any way to do what I am trying to do? I have had success
                > dynamically calling methods on known classes. E.g.,
                >
                > $methodName = 'MyMethod';
                > $result = MyClass::$metho dName();
                >
                > This works fine. What if I want to have the class name dynamically defined
                > as well? Is there any way to do that?
                >
                > Thanks for any pointers!
                >
                >[/color]


                The problem is that $className::$me thodName() results in an error simply
                because $className is a string, not an object.
                $$className won't work because $MyClass is an undefined variable and not
                an object.

                I think eval() might solve that.
                Try something like

                eval($className .'::'.$methodNa me.'();');

                This *should* translate the string into

                MyClass::MyMeth od();

                and execute it.

                I'm sure that eval() will help you out but I haven't tested this.

                --
                Ashmo

                Comment

                Working...