0
0
PHPprogramming~20 mins

First-class callable syntax in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
First-class Callable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
AFatal error: Uncaught Error: Call to undefined function sayHello()
BHello!
CObject of class Closure could not be converted to string
DHello! Hello!
Attempts:
2 left
💡 Hint
The syntax $object->method(...) creates a first-class callable to that method.
Predict Output
intermediate
2: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);
A5
BError: Cannot use first-class callable on static method
C25
DTypeError: Argument 1 passed must be int
Attempts:
2 left
💡 Hint
Static methods can be referenced with ClassName::method(...) syntax.
🔧 Debug
advanced
2: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();
AFatal error: Uncaught ArgumentCountError: Too few arguments to function greet()
BHi, !
CSyntax error: unexpected '...'
DCall to undefined function greet
Attempts:
2 left
💡 Hint
Check how many arguments the callable is called with.
📝 Syntax
advanced
2: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.
A$callable = $obj->run(...);
B$callable = $obj->run;
C$callable = $obj->run();
D$callable = run($obj);
Attempts:
2 left
💡 Hint
First-class callable syntax requires three dots after the method name.
🚀 Application
expert
3: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);
AArray ( [0] => ToUpper [1] => ToUpper [2] => ToUpper )
BArray ( [0] => apple [1] => banana [2] => cherry )
CFatal error: Uncaught TypeError: Argument 1 passed to array_map must be callable
DArray ( [0] => APPLE [1] => BANANA [2] => CHERRY )
Attempts:
2 left
💡 Hint
array_map applies the callable to each element of the array.