Dynamically invoke method / function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    Dynamically invoke method / function

    Say the name of the function is passed by GET.

    How can we invoke the method?

    EDIT: call_user_func( ) does the trick?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    call_user_func( ) should work.

    You could always use the eval function.
    I would put a huge warning sign on the idea of calling functions from a variable passed by get tho!
    Just imagine... page.php?func=u nlink("~/*");
    Dunno if that exact syntax actually works but something similar probably does.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Markus.

      Consider validating your function name.

      E.g.:
      [code=php]
      switch( $_GET['func'] )
      {
      case '':
      $func = 'strip_tags';
      break;

      case 'strip_tags':
      case 'trim':
      case 'rot13':
      $func = $_GET['func'];
      break;

      default:
      $func = 'strip_tags';
      break;
      }

      echo $func($text);
      [/code]

      [EDIT: I can't wait for that issetor operator....]

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        I'm not stupid guys ;]

        Like an MVC, the class is passed through the url and the method follows.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by markusn00b
          Say the name of the function is passed by GET.

          How can we invoke the method?
          Do you mean this...
          [CODE=php]if (function_exist s($_GET["function"]))
          $_GET["function"]();[/CODE]

          You can change it for the function arguments too.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by hsriat
            Do you mean this...
            [CODE=php]if (function_exist s($_GET["function"]))
            $_GET["function"]();[/CODE]

            You can change it for the function arguments too.
            $_GET['func']();
            Looks abit ugly, to me.

            :)

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by markusn00b
              $_GET['func']();
              Looks abit ugly, to me.

              :)
              LOL!!
              Does that count if its working?

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by hsriat
                LOL!!
                Does that count if its working?
                Most certainly does!

                I like my code like my women ;]

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Originally posted by markusn00b
                  Most certainly does!

                  I like my code like my women ;]
                  women? ... interesting...

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Originally posted by hsriat
                    women? ... interesting...
                    Is that an offer?

                    Comment

                    • hsriat
                      Recognized Expert Top Contributor
                      • Jan 2008
                      • 1653

                      #11
                      Originally posted by markusn00b
                      Is that an offer?
                      no.. its already scarcity here.

                      Comment

                      Working...