Anonymous functions create Closure objects that can be stored, passed to APIs and invoked later.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Anonymous Functions and Closures Works
Anonymous functions create Closure objects that can be stored, passed to APIs and invoked later.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$greet = function (string $name): string { return "Hello, $name"; };
echo $greet("Alex");
Output:
Hello, Alex
Anonymous functions create Closure objects that can be stored, passed to APIs and invoked later.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$values = [2, 4, 6];
$transform = fn(int $value): int => $value * 3;
print_r(array_map($transform, $values));
Output:
Array
(
[0] => 6
[1] => 12
[2] => 18
)
Understand variable capture and callable signatures; PHP does not automatically capture locals in traditional closures.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$names = [" Alex", "Sam "];
$normalizeName = function (string $name): string { return trim($name); };
print_r(array_map($normalizeName, $names));
Output:
Array
(
[0] => Alex
[1] => Sam
)
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Understand variable capture and callable signatures; PHP does not automatically capture locals in traditional closures.
- Anonymous functions create Closure objects that can be stored, passed to APIs and invoked later.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Anonymous Functions and Closures | Anonymous functions create Closure objects that can be stored, passed to APIs and invoked later. |
| Alternative approach | Arrow functions provide shorter syntax for simple expressions. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Anonymous Functions and Closures | Understand variable capture and callable signatures; PHP does not automatically capture locals in traditional closures. |
| 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 provide shorter syntax for simple expressions. |
Key Takeaways
- Anonymous functions create Closure objects that can be stored, passed to APIs and invoked later.
- Understand variable capture and callable signatures; PHP does not automatically capture locals in traditional closures.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Anonymous Functions and Closures 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.