Challenge - 5 Problems
First-class Callable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of first-class callable with method reference
What is the output of this PHP code using first-class callable syntax?
PHP
<?php class Greeter { public function sayHello() { return "Hello!"; } } $greeter = new Greeter(); $callable = $greeter->sayHello(...); echo $callable();
Attempts:
2 left
💡 Hint
The syntax $object->method(...) creates a first-class callable to that method.
✗ Incorrect
The code creates a first-class callable to the sayHello method of the Greeter object. Calling $callable() executes sayHello and returns "Hello!".
❓ Predict Output
intermediate2:00remaining
Output of first-class callable with static method
What does this PHP code output when using first-class callable syntax with a static method?
PHP
<?php class MathOps { public static function square(int $x): int { return $x * $x; } } $squareFunc = MathOps::square(...); echo $squareFunc(5);
Attempts:
2 left
💡 Hint
Static methods can be referenced with ClassName::method(...) syntax.
✗ Incorrect
The first-class callable $squareFunc points to MathOps::square. Calling it with 5 returns 25.
🔧 Debug
advanced2:00remaining
Identify the error in first-class callable usage
What error does this PHP code produce?
PHP
<?php function greet(string $name) { return "Hi, $name!"; } $callable = greet(...); echo $callable();
Attempts:
2 left
💡 Hint
Check how many arguments the callable is called with.
✗ Incorrect
The callable expects one argument, but calling $callable() without arguments causes an ArgumentCountError.
📝 Syntax
advanced2:00remaining
Which option correctly creates a first-class callable for an instance method?
Choose the correct PHP syntax to create a first-class callable for the method 'run' of an object $obj.
Attempts:
2 left
💡 Hint
First-class callable syntax requires three dots after the method name.
✗ Incorrect
Option A uses the correct syntax $obj->method(...). Option A is invalid syntax, C calls the method immediately, and D is not valid PHP.
🚀 Application
expert3:00remaining
Using first-class callable in array_map
What is the output of this PHP code that uses a first-class callable with array_map?
PHP
<?php class Converter { public function toUpper(string $text): string { return strtoupper($text); } } $converter = new Converter(); $words = ['apple', 'banana', 'cherry']; $upperWords = array_map($converter->toUpper(...), $words); print_r($upperWords);
Attempts:
2 left
💡 Hint
array_map applies the callable to each element of the array.
✗ Incorrect
The first-class callable $converter->toUpper(...) is passed to array_map, which applies it to each word, converting them to uppercase.