If you're a JavaScript developer, here are the similarities and differences to JS arrow functions:
Same:
- Makes an anonymous function
- Binds the value of "$this" to its value in the parent scope.
- (along with all other variables of the parent scope. See note below)
Different:
- You must write "fn()" instead of just "()"
- The function body is limited to just ONE expression
- So no multi-line function bodies with "{" and "}"
Same and Different at the same time:
- Binds ALL the variables of the parent scope
- In JavaScript all functions are closures, binding to the variables in their parent scope (except for "this").
- But in PHP, normal anonymous functions (defined with "function() {}") do NOT get access to the parent scope, unless they explicitly declare a closure with keyword "use"
- PHP arrow functions, on the other hand, automatically bind to ALL variables in the parent scope. So, this makes them behave the same as JS functions, but be aware that in PHP this is special behavior unique to arrow functions.