Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Closures: Capturing Variables by Value and Reference Works
Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$status = "draft";
$byValue = function () use ($status): string { return $status; };
$status = "published";
echo $byValue();
Output:
draft
Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$status = "draft";
$byReference = function () use (&$status): string { return $status; };
$status = "published";
echo $byReference();
Output:
published
Reference capture introduces shared mutable state, so avoid it unless the callback truly needs to update external state.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$counter = 2;
$byValue = function () use ($counter): int { return $counter; };
$byReference = function () use (&$counter): int { return $counter; };
$counter = 8;
echo $byValue() . ":" . $byReference();
Output:
2:8
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Reference capture introduces shared mutable state, so avoid it unless the callback truly needs to update external state.
- Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Closures: Capturing Variables by Value and Reference | Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable. |
| Alternative approach | Arrow functions implicitly capture used values by value and cannot use the explicit use reference syntax. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Closures: Capturing Variables by Value and Reference | Reference capture introduces shared mutable state, so avoid it unless the callback truly needs to update external state. |
| 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 implicitly capture used values by value and cannot use the explicit use reference syntax. |
Key Takeaways
- Capturing by value snapshots a variable, while capturing by reference observes and can mutate the same outside variable.
- Reference capture introduces shared mutable state, so avoid it unless the callback truly needs to update external state.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Closures: Capturing Variables by Value and Reference 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.