Time Of Closure Creation¶
Here are four ways to create a closure : a callable array, an arrow function, a first class callable, and a static method. All of them produce the same feature. Here, we focus on the time of checks.
The callable array only collect the variables: the check on these values happen at call time.
The arrow function also collects the values, and report an error only at execution time.
The first class callable checks for the values, reports a failing call to the non-existing method: all this, at definition time. The values must be consistent then.
The static method Closure::fromCallable() also checks the values at its execution time (not the resulting closure’s), and yield a more sensible error message: ailed to create closure from callable: class stdClass does not have a method "foo".
Which one is your favorite?
See Also¶
Potato, potato, potato… [Try me]