First-class callable syntax lets you treat functions like variables. You can store, pass, and use them easily.
First-class callable syntax in PHP
$callable = function_name(...);
Use the function name followed by (...) to get a callable.
This syntax was introduced in PHP 8.1 for cleaner and simpler code.
strlen function as a callable and use it.$strlenCallable = strlen(...); echo $strlenCallable('hello');
greet as a callable and call it.function greet($name) { return "Hello, $name!"; } $greetCallable = greet(...); echo $greetCallable('Alice');
array_map to double numbers.$numbers = [1, 2, 3]; $double = fn($n) => $n * 2; $doubledNumbers = array_map($double, $numbers); print_r($doubledNumbers);
trim to clean array strings.$trimCallable = trim(...); $words = [' apple ', ' banana ', ' cherry ']; $trimmed = array_map($trimCallable, $words); print_r($trimmed);
This program defines a function square that returns the square of a number. It then stores this function as a callable using first-class callable syntax. Using array_map, it applies the callable to each number in the array and prints the results.
<?php function square($x) { return $x * $x; } $squareCallable = square(...); $values = [2, 3, 4]; $squaredValues = array_map($squareCallable, $values); foreach ($squaredValues as $value) { echo $value . "\n"; }
First-class callable syntax only works with named functions or methods, not with anonymous functions directly.
You can also use this syntax with class methods like ClassName::methodName(...).
This syntax makes code cleaner and easier to read compared to older ways of creating callables.
First-class callable syntax lets you treat functions like variables easily.
Use function_name(...) to get a callable for that function.
This helps when passing functions as arguments or storing them for later use.
Practice
Solution
Step 1: Understand the concept of first-class callables
First-class callable syntax lets you treat functions like variables, so you can pass or store them.Step 2: Compare options with this concept
Only 'Treat functions as variables to pass or store them easily' correctly describes this ability; others describe unrelated features.Final Answer:
Treat functions as variables to pass or store them easily -> Option DQuick Check:
First-class callable = Treat functions as variables [OK]
- Confusing callable syntax with dynamic function creation
- Thinking it converts functions to strings
- Assuming it restricts function calls
foo in PHP?Solution
Step 1: Recall the first-class callable syntax
In PHP, to get a callable for a function, you usefunction_name(...).Step 2: Match the syntax with options
$callable = foo(...); usesfoo(...), which is correct. $callable = foo(); calls the function. foo\(...) uses backslash incorrectly causing syntax error. $callable = foo; treats name as constant.Final Answer:
$callable = foo(...); -> Option AQuick Check:
Callable syntax = function_name(...) [OK]
- Calling the function instead of getting callable
- Omitting parentheses and ellipsis
- Using backslash incorrectly for global functions
$fn = strlen(...);
echo $fn('hello');Solution
Step 1: Understand the callable assignment
The variable$fnis assigned the callable for the built-in functionstrlenusing first-class callable syntax.Step 2: Call the callable with argument 'hello'
Calling$fn('hello')executesstrlen('hello'), which returns 5.Final Answer:
5 -> Option AQuick Check:
strlen('hello') = 5 [OK]
- Expecting the function name as output
- Confusing callable with string
- Thinking it causes an error
function greet($name) {
return "Hello, $name!";
}
$greet = greet;
echo $greet('World');
Solution
Step 1: Check how callable is assigned
The code assigns$greet = greet;which is invalid syntax for first-class callable.Step 2: Correct syntax requires parentheses and ellipsis
To get a callable, it should be$greet = greet(...);. Without this, PHP treats it as a constant or causes error.Final Answer:
Missing parentheses and ellipsis to get callable -> Option BQuick Check:
Callable syntax needs (...) [OK]
- Assigning function name without parentheses
- Assuming function name alone is callable
- Ignoring syntax errors
function multiply($a, $b) {
return $a * $b;
}
$operations = [
'double' => multiply(...),
'square' => fn($x) => multiply($x, $x),
];
echo $operations['double'](5, 2) . ', ' . $operations['square'](4);
Solution
Step 1: Understand the callable assignments
The array$operationsstores two callables: 'double' uses first-class callablemultiply(...), 'square' uses an arrow function calling multiply.Step 2: Evaluate each callable call
$operations['double'](5, 2)calls multiply(5, 2) = 10.$operations['square'](4)calls multiply(4, 4) = 16.Final Answer:
10, 16 -> Option CQuick Check:
multiply(5,2)=10 and multiply(4,4)=16 [OK]
- Confusing arrow function with callable syntax
- Expecting error from multiply(...) usage
- Mixing up multiply arguments
Ready to prove it?
Pass the test and this pattern moves unknown
