Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the keyword "callable" do in PHP

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

Edit: Here's a link to where I copied the above piece of code from: link

like image 794
S. Goody Avatar asked Jan 13 '19 06:01

S. Goody


3 Answers

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.

like image 90
entpnerd Avatar answered Oct 23 '22 17:10

entpnerd


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.

like image 36
Shahin Avatar answered Oct 23 '22 18:10

Shahin


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
)
like image 3
Progrock Avatar answered Oct 23 '22 17:10

Progrock