What does the keyword “callable” do in PHP












7















To be more exact, the "callable" used in function declaration arguments. like the one below.



function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}


How does it benefit us?



why and how do we use it?



Maybe this is very basic for you, however, I've tried searching for it and I was getting no answers. at least, nothing I could understand.



Hoping for a for-dummies answer. I'm new to coding... XD










share|improve this question









New contributor




S. Goody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3





    callable is just a type hint for the parameter - have a look at php.net/manual/en/language.types.callable.php for what a callable is.

    – Nigel Ren
    1 hour ago
















7















To be more exact, the "callable" used in function declaration arguments. like the one below.



function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}


How does it benefit us?



why and how do we use it?



Maybe this is very basic for you, however, I've tried searching for it and I was getting no answers. at least, nothing I could understand.



Hoping for a for-dummies answer. I'm new to coding... XD










share|improve this question









New contributor




S. Goody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3





    callable is just a type hint for the parameter - have a look at php.net/manual/en/language.types.callable.php for what a callable is.

    – Nigel Ren
    1 hour ago














7












7








7








To be more exact, the "callable" used in function declaration arguments. like the one below.



function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}


How does it benefit us?



why and how do we use it?



Maybe this is very basic for you, however, I've tried searching for it and I was getting no answers. at least, nothing I could understand.



Hoping for a for-dummies answer. I'm new to coding... XD










share|improve this question









New contributor




S. Goody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












To be more exact, the "callable" used in function declaration arguments. like the one below.



function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}


How does it benefit us?



why and how do we use it?



Maybe this is very basic for you, however, I've tried searching for it and I was getting no answers. at least, nothing I could understand.



Hoping for a for-dummies answer. I'm new to coding... XD







php class callable function-declaration






share|improve this question









New contributor




S. Goody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




S. Goody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 31 mins ago







S. Goody













New contributor




S. Goody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









S. GoodyS. Goody

363




363




New contributor




S. Goody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





S. Goody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






S. Goody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 3





    callable is just a type hint for the parameter - have a look at php.net/manual/en/language.types.callable.php for what a callable is.

    – Nigel Ren
    1 hour ago














  • 3





    callable is just a type hint for the parameter - have a look at php.net/manual/en/language.types.callable.php for what a callable is.

    – Nigel Ren
    1 hour ago








3




3





callable is just a type hint for the parameter - have a look at php.net/manual/en/language.types.callable.php for what a callable is.

– Nigel Ren
1 hour ago





callable is just a type hint for the parameter - have a look at php.net/manual/en/language.types.callable.php for what a callable is.

– Nigel Ren
1 hour ago












4 Answers
4






active

oldest

votes


















1














A callable (callback) function is a function that is called inside another function or used as a parameter of another function



// An example callback function
function my_callback_function() {
echo 'hello world!';
}

// Type 1: Simple callback
call_user_func('my_callback_function');


There are some cases that your function is a template for other functions, in that case, you use parameters for the callable function.




for more information:
http://php.net/manual/en/language.types.callable.php







share|improve this answer































    1














    The callable type allows us to pass a callback function to the function that is being called. That is, callback function parameters allow the function being called to dynamically call code that we specify in the callable function parameter. This is useful because it allows us to pass dynamic code to be executed to a function.



    For example, one might want to call a function and the function accepts a callback function called log, which would log data in a custom way that you want.



    I hope that makes sense. For details, see this link.






    share|improve this answer































      1














      It's a type hinting which tells us this function accepts the parameter $handler as a function, see this example to clarify things:



      function helloWorld()
      {
      echo 'Hello World!';
      }
      function handle(Callable $fn)
      {
      $fn(); // We know the parameter is callable then we execute the function.
      }

      handle('helloWorld'); // Outputs: Hello World!


      It's a very simple example, But I hope it helps you understand the idea.






      share|improve this answer































        0














        Here is example use of using a callable as a parameter.



        The wait_do_linebreak function below will sleep for a given time, then call a function with the tailing parameters given, and then echo a line break.



        ...$params packs the tailing parameters into an array called $params. Here it's being used to proxy arguments into the callables.



        At the end of the examples you'll see a native function that takes a callable as a parameter.



        <?php

        function wait_do_linebreak($time, callable $something, ...$params)
        {
        sleep($time);
        call_user_func_array($something, $params);
        echo "n";
        }

        function earth_greeting() {
        echo 'hello earth';
        }

        class Echo_Two
        {
        public function __invoke($baz, $bat)
        {
        echo $baz, " ", $bat;
        }
        }

        class Eat_Static
        {
        static function another()
        {
        echo 'Another example.';
        }
        }

        class Foo
        {
        public function more()
        {
        echo 'And here is another one.';
        }
        }

        wait_do_linebreak(0, 'earth_greeting');
        $my_echo = function($str) {
        echo $str;
        };
        wait_do_linebreak(0, $my_echo, 'hello');
        wait_do_linebreak(0, function() {
        echo "I'm on top of the world.";
        });
        wait_do_linebreak(0, new Echo_Two, 'The', 'Earth');
        wait_do_linebreak(0, ['Eat_Static', 'another']);
        wait_do_linebreak(0, [new Foo, 'more']);

        $array = [
        'jim',
        'bones',
        'spock'
        ];

        $word_contains_o = function (string $str) {
        return strpos($str, 'o') !== false;
        };
        print_r(array_filter($array, $word_contains_o));


        Output:



        hello earth
        hello
        I'm on top of the world.
        The Earth
        Another example.
        And here is another one.
        Array
        (
        [1] => bones
        [2] => spock
        )





        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          S. Goody is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54166666%2fwhat-does-the-keyword-callable-do-in-php%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          A callable (callback) function is a function that is called inside another function or used as a parameter of another function



          // An example callback function
          function my_callback_function() {
          echo 'hello world!';
          }

          // Type 1: Simple callback
          call_user_func('my_callback_function');


          There are some cases that your function is a template for other functions, in that case, you use parameters for the callable function.




          for more information:
          http://php.net/manual/en/language.types.callable.php







          share|improve this answer




























            1














            A callable (callback) function is a function that is called inside another function or used as a parameter of another function



            // An example callback function
            function my_callback_function() {
            echo 'hello world!';
            }

            // Type 1: Simple callback
            call_user_func('my_callback_function');


            There are some cases that your function is a template for other functions, in that case, you use parameters for the callable function.




            for more information:
            http://php.net/manual/en/language.types.callable.php







            share|improve this answer


























              1












              1








              1







              A callable (callback) function is a function that is called inside another function or used as a parameter of another function



              // An example callback function
              function my_callback_function() {
              echo 'hello world!';
              }

              // Type 1: Simple callback
              call_user_func('my_callback_function');


              There are some cases that your function is a template for other functions, in that case, you use parameters for the callable function.




              for more information:
              http://php.net/manual/en/language.types.callable.php







              share|improve this answer













              A callable (callback) function is a function that is called inside another function or used as a parameter of another function



              // An example callback function
              function my_callback_function() {
              echo 'hello world!';
              }

              // Type 1: Simple callback
              call_user_func('my_callback_function');


              There are some cases that your function is a template for other functions, in that case, you use parameters for the callable function.




              for more information:
              http://php.net/manual/en/language.types.callable.php








              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 1 hour ago









              M.O.AM.O.A

              153




              153

























                  1














                  The callable type allows us to pass a callback function to the function that is being called. That is, callback function parameters allow the function being called to dynamically call code that we specify in the callable function parameter. This is useful because it allows us to pass dynamic code to be executed to a function.



                  For example, one might want to call a function and the function accepts a callback function called log, which would log data in a custom way that you want.



                  I hope that makes sense. For details, see this link.






                  share|improve this answer




























                    1














                    The callable type allows us to pass a callback function to the function that is being called. That is, callback function parameters allow the function being called to dynamically call code that we specify in the callable function parameter. This is useful because it allows us to pass dynamic code to be executed to a function.



                    For example, one might want to call a function and the function accepts a callback function called log, which would log data in a custom way that you want.



                    I hope that makes sense. For details, see this link.






                    share|improve this answer


























                      1












                      1








                      1







                      The callable type allows us to pass a callback function to the function that is being called. That is, callback function parameters allow the function being called to dynamically call code that we specify in the callable function parameter. This is useful because it allows us to pass dynamic code to be executed to a function.



                      For example, one might want to call a function and the function accepts a callback function called log, which would log data in a custom way that you want.



                      I hope that makes sense. For details, see this link.






                      share|improve this answer













                      The callable type allows us to pass a callback function to the function that is being called. That is, callback function parameters allow the function being called to dynamically call code that we specify in the callable function parameter. This is useful because it allows us to pass dynamic code to be executed to a function.



                      For example, one might want to call a function and the function accepts a callback function called log, which would log data in a custom way that you want.



                      I hope that makes sense. For details, see this link.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 1 hour ago









                      entpnerdentpnerd

                      4,77721843




                      4,77721843























                          1














                          It's a type hinting which tells us this function accepts the parameter $handler as a function, see this example to clarify things:



                          function helloWorld()
                          {
                          echo 'Hello World!';
                          }
                          function handle(Callable $fn)
                          {
                          $fn(); // We know the parameter is callable then we execute the function.
                          }

                          handle('helloWorld'); // Outputs: Hello World!


                          It's a very simple example, But I hope it helps you understand the idea.






                          share|improve this answer




























                            1














                            It's a type hinting which tells us this function accepts the parameter $handler as a function, see this example to clarify things:



                            function helloWorld()
                            {
                            echo 'Hello World!';
                            }
                            function handle(Callable $fn)
                            {
                            $fn(); // We know the parameter is callable then we execute the function.
                            }

                            handle('helloWorld'); // Outputs: Hello World!


                            It's a very simple example, But I hope it helps you understand the idea.






                            share|improve this answer


























                              1












                              1








                              1







                              It's a type hinting which tells us this function accepts the parameter $handler as a function, see this example to clarify things:



                              function helloWorld()
                              {
                              echo 'Hello World!';
                              }
                              function handle(Callable $fn)
                              {
                              $fn(); // We know the parameter is callable then we execute the function.
                              }

                              handle('helloWorld'); // Outputs: Hello World!


                              It's a very simple example, But I hope it helps you understand the idea.






                              share|improve this answer













                              It's a type hinting which tells us this function accepts the parameter $handler as a function, see this example to clarify things:



                              function helloWorld()
                              {
                              echo 'Hello World!';
                              }
                              function handle(Callable $fn)
                              {
                              $fn(); // We know the parameter is callable then we execute the function.
                              }

                              handle('helloWorld'); // Outputs: Hello World!


                              It's a very simple example, But I hope it helps you understand the idea.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 1 hour ago









                              ShahinShahin

                              33116




                              33116























                                  0














                                  Here is example use of using a callable as a parameter.



                                  The wait_do_linebreak function below will sleep for a given time, then call a function with the tailing parameters given, and then echo a line break.



                                  ...$params packs the tailing parameters into an array called $params. Here it's being used to proxy arguments into the callables.



                                  At the end of the examples you'll see a native function that takes a callable as a parameter.



                                  <?php

                                  function wait_do_linebreak($time, callable $something, ...$params)
                                  {
                                  sleep($time);
                                  call_user_func_array($something, $params);
                                  echo "n";
                                  }

                                  function earth_greeting() {
                                  echo 'hello earth';
                                  }

                                  class Echo_Two
                                  {
                                  public function __invoke($baz, $bat)
                                  {
                                  echo $baz, " ", $bat;
                                  }
                                  }

                                  class Eat_Static
                                  {
                                  static function another()
                                  {
                                  echo 'Another example.';
                                  }
                                  }

                                  class Foo
                                  {
                                  public function more()
                                  {
                                  echo 'And here is another one.';
                                  }
                                  }

                                  wait_do_linebreak(0, 'earth_greeting');
                                  $my_echo = function($str) {
                                  echo $str;
                                  };
                                  wait_do_linebreak(0, $my_echo, 'hello');
                                  wait_do_linebreak(0, function() {
                                  echo "I'm on top of the world.";
                                  });
                                  wait_do_linebreak(0, new Echo_Two, 'The', 'Earth');
                                  wait_do_linebreak(0, ['Eat_Static', 'another']);
                                  wait_do_linebreak(0, [new Foo, 'more']);

                                  $array = [
                                  'jim',
                                  'bones',
                                  'spock'
                                  ];

                                  $word_contains_o = function (string $str) {
                                  return strpos($str, 'o') !== false;
                                  };
                                  print_r(array_filter($array, $word_contains_o));


                                  Output:



                                  hello earth
                                  hello
                                  I'm on top of the world.
                                  The Earth
                                  Another example.
                                  And here is another one.
                                  Array
                                  (
                                  [1] => bones
                                  [2] => spock
                                  )





                                  share|improve this answer




























                                    0














                                    Here is example use of using a callable as a parameter.



                                    The wait_do_linebreak function below will sleep for a given time, then call a function with the tailing parameters given, and then echo a line break.



                                    ...$params packs the tailing parameters into an array called $params. Here it's being used to proxy arguments into the callables.



                                    At the end of the examples you'll see a native function that takes a callable as a parameter.



                                    <?php

                                    function wait_do_linebreak($time, callable $something, ...$params)
                                    {
                                    sleep($time);
                                    call_user_func_array($something, $params);
                                    echo "n";
                                    }

                                    function earth_greeting() {
                                    echo 'hello earth';
                                    }

                                    class Echo_Two
                                    {
                                    public function __invoke($baz, $bat)
                                    {
                                    echo $baz, " ", $bat;
                                    }
                                    }

                                    class Eat_Static
                                    {
                                    static function another()
                                    {
                                    echo 'Another example.';
                                    }
                                    }

                                    class Foo
                                    {
                                    public function more()
                                    {
                                    echo 'And here is another one.';
                                    }
                                    }

                                    wait_do_linebreak(0, 'earth_greeting');
                                    $my_echo = function($str) {
                                    echo $str;
                                    };
                                    wait_do_linebreak(0, $my_echo, 'hello');
                                    wait_do_linebreak(0, function() {
                                    echo "I'm on top of the world.";
                                    });
                                    wait_do_linebreak(0, new Echo_Two, 'The', 'Earth');
                                    wait_do_linebreak(0, ['Eat_Static', 'another']);
                                    wait_do_linebreak(0, [new Foo, 'more']);

                                    $array = [
                                    'jim',
                                    'bones',
                                    'spock'
                                    ];

                                    $word_contains_o = function (string $str) {
                                    return strpos($str, 'o') !== false;
                                    };
                                    print_r(array_filter($array, $word_contains_o));


                                    Output:



                                    hello earth
                                    hello
                                    I'm on top of the world.
                                    The Earth
                                    Another example.
                                    And here is another one.
                                    Array
                                    (
                                    [1] => bones
                                    [2] => spock
                                    )





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Here is example use of using a callable as a parameter.



                                      The wait_do_linebreak function below will sleep for a given time, then call a function with the tailing parameters given, and then echo a line break.



                                      ...$params packs the tailing parameters into an array called $params. Here it's being used to proxy arguments into the callables.



                                      At the end of the examples you'll see a native function that takes a callable as a parameter.



                                      <?php

                                      function wait_do_linebreak($time, callable $something, ...$params)
                                      {
                                      sleep($time);
                                      call_user_func_array($something, $params);
                                      echo "n";
                                      }

                                      function earth_greeting() {
                                      echo 'hello earth';
                                      }

                                      class Echo_Two
                                      {
                                      public function __invoke($baz, $bat)
                                      {
                                      echo $baz, " ", $bat;
                                      }
                                      }

                                      class Eat_Static
                                      {
                                      static function another()
                                      {
                                      echo 'Another example.';
                                      }
                                      }

                                      class Foo
                                      {
                                      public function more()
                                      {
                                      echo 'And here is another one.';
                                      }
                                      }

                                      wait_do_linebreak(0, 'earth_greeting');
                                      $my_echo = function($str) {
                                      echo $str;
                                      };
                                      wait_do_linebreak(0, $my_echo, 'hello');
                                      wait_do_linebreak(0, function() {
                                      echo "I'm on top of the world.";
                                      });
                                      wait_do_linebreak(0, new Echo_Two, 'The', 'Earth');
                                      wait_do_linebreak(0, ['Eat_Static', 'another']);
                                      wait_do_linebreak(0, [new Foo, 'more']);

                                      $array = [
                                      'jim',
                                      'bones',
                                      'spock'
                                      ];

                                      $word_contains_o = function (string $str) {
                                      return strpos($str, 'o') !== false;
                                      };
                                      print_r(array_filter($array, $word_contains_o));


                                      Output:



                                      hello earth
                                      hello
                                      I'm on top of the world.
                                      The Earth
                                      Another example.
                                      And here is another one.
                                      Array
                                      (
                                      [1] => bones
                                      [2] => spock
                                      )





                                      share|improve this answer













                                      Here is example use of using a callable as a parameter.



                                      The wait_do_linebreak function below will sleep for a given time, then call a function with the tailing parameters given, and then echo a line break.



                                      ...$params packs the tailing parameters into an array called $params. Here it's being used to proxy arguments into the callables.



                                      At the end of the examples you'll see a native function that takes a callable as a parameter.



                                      <?php

                                      function wait_do_linebreak($time, callable $something, ...$params)
                                      {
                                      sleep($time);
                                      call_user_func_array($something, $params);
                                      echo "n";
                                      }

                                      function earth_greeting() {
                                      echo 'hello earth';
                                      }

                                      class Echo_Two
                                      {
                                      public function __invoke($baz, $bat)
                                      {
                                      echo $baz, " ", $bat;
                                      }
                                      }

                                      class Eat_Static
                                      {
                                      static function another()
                                      {
                                      echo 'Another example.';
                                      }
                                      }

                                      class Foo
                                      {
                                      public function more()
                                      {
                                      echo 'And here is another one.';
                                      }
                                      }

                                      wait_do_linebreak(0, 'earth_greeting');
                                      $my_echo = function($str) {
                                      echo $str;
                                      };
                                      wait_do_linebreak(0, $my_echo, 'hello');
                                      wait_do_linebreak(0, function() {
                                      echo "I'm on top of the world.";
                                      });
                                      wait_do_linebreak(0, new Echo_Two, 'The', 'Earth');
                                      wait_do_linebreak(0, ['Eat_Static', 'another']);
                                      wait_do_linebreak(0, [new Foo, 'more']);

                                      $array = [
                                      'jim',
                                      'bones',
                                      'spock'
                                      ];

                                      $word_contains_o = function (string $str) {
                                      return strpos($str, 'o') !== false;
                                      };
                                      print_r(array_filter($array, $word_contains_o));


                                      Output:



                                      hello earth
                                      hello
                                      I'm on top of the world.
                                      The Earth
                                      Another example.
                                      And here is another one.
                                      Array
                                      (
                                      [1] => bones
                                      [2] => spock
                                      )






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 15 mins ago









                                      ProgrockProgrock

                                      4,2161820




                                      4,2161820






















                                          S. Goody is a new contributor. Be nice, and check out our Code of Conduct.










                                          draft saved

                                          draft discarded


















                                          S. Goody is a new contributor. Be nice, and check out our Code of Conduct.













                                          S. Goody is a new contributor. Be nice, and check out our Code of Conduct.












                                          S. Goody is a new contributor. Be nice, and check out our Code of Conduct.
















                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54166666%2fwhat-does-the-keyword-callable-do-in-php%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          What other Star Trek series did the main TNG cast show up in?

                                          Berlina muro

                                          Berlina aerponto