Type hinting – Difference between `Closure` and `Callable`
The difference is, that a Closure
must be an anonymous function, where callable
also can be a normal function.
You can see/test this with the example below and you will see that you will get an error for the first one:
function callFunc1(Closure $closure) {
$closure();
}
function callFunc2(Callable $callback) {
$callback();
}
function xy() {
echo 'Hello, World!';
}
callFunc1("xy"); // Catchable fatal error: Argument 1 passed to callFunc1() must be an instance of Closure, string given
callFunc2("xy"); // Hello, World!
So if you only want to type hint anonymous function use: Closure
and if you want also to allow normal functions use callable
as type hint.
php type hint for type of function or closure
It's callable
:
function FuncA (integer $A, array $B, callable $C){
$C();
}
What does the keyword callable do in PHP
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.
Is type callable supported with typed properties?
Proposal in provided link https://wiki.php.net/rfc/typed-properties has status declined.
The proposal implemented in php7.4 is here https://wiki.php.net/rfc/typed_properties_v2 and there's an explanation about callable
:
The callable type is not supported, because its behavior is context
dependent The following example illustrates the issue:class Test {
public callable $cb;
public function __construct() {
// $this->cb is callable here
$this->cb = [$this, 'method'];
}
private function method() {}
}
$obj = new Test;
// $obj->cb is NOT callable here
($obj->cb)();
This means that it is possible to write a legal value to a property
and then proceed to read an illegal value from the same property. This
fundamental problem of thecallable
pseudo-type is laid out in much
more detail in the consistent callables RFC.The recommended workaround is to instead use the
Closure
type, in
conjunction withClosure::fromCallable()
. This ensures that the
callable will remain callable independent of scope. For a discussion
of alternative ways to handle the callable issue, see the Alternatives
section.
List of all implemented proposals for php7.4 is here https://wiki.php.net/rfc#php_74.
Theory - Can we consider a callable PHP being a runnable?
It is my understanding that a callable represents some form of data the php interprets to be a method invocation. It does not mean that a callable has immediately executable code, java's does.
PHP will then take what parameters it has and goes and FINDS the code and executes it. I think that is the biggest difference. Sometimes there is immediately executable code, sometimes it has to lookup where the code is "\someclass::somestaticmethod"
My thoughts.
Related Topics
Update Xampp from Maria Db 10.1 to 10.2
MySQL Database Won't Start in Xampp Manager-Osx
Efficient PHP Auto-Loading and Naming Strategies
When to Generate a New Application Key in Laravel
Using Zend Framework for Highload Projects
Dynamically Load Information to Twitter Bootstrap Modal
How to Trigger Xdebug Profiler for a Command Line PHP Script
How to Start a Get/Post/Put/Delete Request and Judge Request Type in PHP
Insert Non-English Decimal Points in MySQL
Printing a New Line in a CSV File Cell
Check If a "Run-Time" Multidimensional Array Key Exists
In Laravel How to Add Values to a Request Array
Checking If an Instance's Class Implements an Interface
Comparing Bcrypt Hash Between PHP and Nodejs