Arrow functions provide expression-only closures with implicit by-value capture, while traditional closures support statements and explicit use clauses.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Closures vs Arrow Functions Works
Arrow functions provide expression-only closures with implicit by-value capture, while traditional closures support statements and explicit use clauses.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$discount = 5;
$arrow = fn(int $price): int => $price - $discount;
echo $arrow(30);
Output:
25
Arrow functions provide expression-only closures with implicit by-value capture, while traditional closures support statements and explicit use clauses.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$discount = 8;
$callback = function (int $price) use ($discount): int { return $price - $discount; };
echo $callback(30);
Output:
22
Arrow-function capture does not update surrounding scalar variables; traditional closures need use (&$variable) for that effect.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$multiplier = 3;
$arrow = fn(int $number): int => $number * $multiplier;
$closure = function (int $number) use ($multiplier): int { return $number * $multiplier; };
echo $arrow(4) . ":" . $closure(4);
Output:
12:12
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Arrow-function capture does not update surrounding scalar variables; traditional closures need use (&$variable) for that effect.
- Arrow functions provide expression-only closures with implicit by-value capture, while traditional closures support statements and explicit use clauses.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Closures vs Arrow Functions | Arrow functions provide expression-only closures with implicit by-value capture, while traditional closures support statements and explicit use clauses. |
| Alternative approach | Use arrow functions for concise transformations and closures for branching or mutable captures. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Closures vs Arrow Functions | Arrow-function capture does not update surrounding scalar variables; traditional closures need use (&$variable) for that effect. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | Use arrow functions for concise transformations and closures for branching or mutable captures. |
Key Takeaways
- Arrow functions provide expression-only closures with implicit by-value capture, while traditional closures support statements and explicit use clauses.
- Arrow-function capture does not update surrounding scalar variables; traditional closures need use (&$variable) for that effect.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Closures vs Arrow Functions is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.