The use clause explicitly captures local variables into a traditional closure, by value or by reference.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Closures with use and Captured Variables Works
The use clause explicitly captures local variables into a traditional closure, by value or by reference.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$currency = "EUR";
$formatter = function (int $amount) use ($currency): string { return "$amount $currency"; };
echo $formatter(12);
Output:
12 EUR
The use clause explicitly captures local variables into a traditional closure, by value or by reference.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$counter = 0;
$increment = function () use (&$counter): void { $counter++; };
$increment();
echo $counter;
Output:
1
A by-value capture preserves its value at closure creation, not at a later call.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$prefix = "ITEM-";
$format = function (int $identifier) use ($prefix): string { return $prefix . $identifier; };
echo $format(14);
Output:
ITEM-14
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- A by-value capture preserves its value at closure creation, not at a later call.
- The use clause explicitly captures local variables into a traditional closure, by value or by reference.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Closures with use and Captured Variables | The use clause explicitly captures local variables into a traditional closure, by value or by reference. |
| Alternative approach | Arrow functions capture used local variables by value without a use clause. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Closures with use and Captured Variables | A by-value capture preserves its value at closure creation, not at a later call. |
| 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 | Arrow functions capture used local variables by value without a use clause. |
Key Takeaways
- The use clause explicitly captures local variables into a traditional closure, by value or by reference.
- A by-value capture preserves its value at closure creation, not at a later call.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Closures with use and Captured Variables 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.