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
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.
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.
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With