First-class callable syntax method(...) creates a Closure from a function or method reference.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Callable Types and First-Class Callables Works
First-class callable syntax method(...) creates a Closure from a function or method reference.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
function prefixCode(string $code): string { return "ID-" . $code; }
$callback = prefixCode(...);
echo $callback("42");
Output:
ID-42
First-class callable syntax method(...) creates a Closure from a function or method reference.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
class Slugger { public function slug(string $text): string { return strtolower(str_replace(" ", "-", $text)); } }
$slugger = new Slugger();
$callback = $slugger->slug(...);
echo $callback("New Article");
Output:
new-article
It captures the callable target at creation and is not the same as invoking that function with spread arguments.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
function calculateTax(float $price): float { return $price * 0.2; }
$taxFunction = calculateTax(...);
function calculate(callable $calculator, float $price): float { return $calculator($price); }
echo calculate($taxFunction, 100);
Output:
20
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- It captures the callable target at creation and is not the same as invoking that function with spread arguments.
- First-class callable syntax method(...) creates a Closure from a function or method reference.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Callable Types and First-Class Callables | First-class callable syntax method(...) creates a Closure from a function or method reference. |
| Alternative approach | callable type declarations accept several callable forms; a first-class callable produces a Closure object. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Callable Types and First-Class Callables | It captures the callable target at creation and is not the same as invoking that function with spread arguments. |
| 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 | callable type declarations accept several callable forms; a first-class callable produces a Closure object. |
Key Takeaways
- First-class callable syntax method(...) creates a Closure from a function or method reference.
- It captures the callable target at creation and is not the same as invoking that function with spread arguments.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Callable Types and 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.