Is it possible that pass a function name as a argument in an other function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iskhan
    New Member
    • Apr 2010
    • 20

    #1

    Is it possible that pass a function name as a argument in an other function?

    Is it possible that pass a function name as a argument in an other function?

    e.g.
    Code:
    MainMenu($SubMenu)
    {
    //Main menu designing start
    if(.......&&......||.......&&.......)
    {
    $this->$Submenu; // different submenus
    exit;
    }
    //Main menu designing end
    }
  • chathura86
    New Member
    • May 2007
    • 227

    #2
    $this->$Submenu() should work i guess,

    but there are php functions for that



    and etc...

    Regards

    Comment

    • iskhan
      New Member
      • Apr 2010
      • 20

      #3
      Error on $
      $this->$Submenu()

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        The $Submenu variable you are using is spelled $SubMenu in your function parameter list. (The M is upper-case in the parameter list.)

        You can use both class and function name strings to construct/execute them.
        For example:
        [code=php]<?php
        class foo
        {
        function bar() {
        echo "Hello, world!";
        }
        }

        $class_name = "foo";
        $func_name = "bar";

        $inst = new $class_name;
        $inst->$func_name() ;
        ?>[/code]
        This prints "Hello, world!".

        Comment

        • iskhan
          New Member
          • Apr 2010
          • 20

          #5
          this is typing mistake.my question is still there.
          Is it possible that pass a function name as a argument in an other function?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Your question has been answered sufficiently. Please review the replies to this thread.

            Comment

            • iskhan
              New Member
              • Apr 2010
              • 20

              #7
              Where is the function which call bar() as an argument?
              What about if ten functions pass as an argument one by one?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Where is the function which call bar() as an argument?
                there ain’t, because the call is executed in global scope. in the end, you pass the name (a string) and then execute that by adding ();

                What about if ten functions pass as an argument one by one?
                I don’t see the problem. (except I totally misunderstand your problem)

                Comment

                • chathura86
                  New Member
                  • May 2007
                  • 227

                  #9
                  have you get a chance to read those links i sent, i think that will explain all your problems

                  Regards

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Code:
                    function call_custom_function($function_name, $arg1, $arg2, $arg3) {
                        if (function_exists($function_name)) {
                            $result = $function_name($arg1, $arg2, $arg3);
                            //function (that was passed as argument) executed
                        }
                        //do something if function does not exist
                    }

                    Comment

                    • iskhan
                      New Member
                      • Apr 2010
                      • 20

                      #11
                      Originally posted by hsriat
                      Code:
                      function call_custom_function($function_name, $arg1, $arg2, $arg3) {
                          if (function_exists($function_name)) {
                              $result = $function_name($arg1, $arg2, $arg3);
                              //function (that was passed as argument) executed
                          }
                          //do something if function does not exist
                      }
                      Yes this is the answer I required. Thanks.
                      One more thing if this function is in the base class and argument functions are in the sub class then where I define the assignment of argument functions and how?

                      Comment

                      Working...