First-class callable syntax creates a Closure from an existing function or object method without invoking it immediately.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP First-Class Callables Works
First-class callable syntax creates a Closure from an existing function or object method without invoking it immediately.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$formatter = trim(...);
echo $formatter(" hello ");
Output:
hello
First-class callable syntax creates a Closure from an existing function or object method without invoking it immediately.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
class HtmlFormatter { public function escape(string $text): string { return htmlspecialchars($text, ENT_QUOTES, "UTF-8"); } }
$formatter = new HtmlFormatter();
$escape = $formatter->escape(...);
echo $escape("<b>hello</b>");
Output:
<b>hello</b>
Do not confuse method(...) callable creation with calling method(...$arguments) using unpacked values.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$formatter = strtoupper(...);
echo $formatter("hello");
Output:
HELLO
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Do not confuse method(...) callable creation with calling method(...$arguments) using unpacked values.
- First-class callable syntax creates a Closure from an existing function or object method without invoking it immediately.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP First-Class Callables | First-class callable syntax creates a Closure from an existing function or object method without invoking it immediately. |
| Alternative approach | Closure::fromCallable() offers a related explicit conversion API. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP First-Class Callables | Do not confuse method(...) callable creation with calling method(...$arguments) using unpacked values. |
| 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 | Closure::fromCallable() offers a related explicit conversion API. |
Key Takeaways
- First-class callable syntax creates a Closure from an existing function or object method without invoking it immediately.
- Do not confuse method(...) callable creation with calling method(...$arguments) using unpacked values.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP First-Class Callables 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.