0
0
PHPprogramming~10 mins

First-class callable syntax in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a first-class callable from the function strlen.

PHP
<?php
$func = [1](...);
echo $func('hello');
?>
Drag options to blanks, or click blank then click option'
Astrlen()
Bstrlen
C'strlen'
D&strlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the function name creates a string, not suitable for first-class syntax.
Adding parentheses calls the function immediately instead of creating a callable.
2fill in blank
medium

Complete the code to create a first-class callable from the function strtoupper using the callable syntax.

PHP
<?php
$func = [1](...);
echo $func('hello');
?>
Drag options to blanks, or click blank then click option'
Astrtoupper
B&strtoupper
Cstrtoupper()
D'strtoupper'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes or parentheses instead of just the function name.
Trying to use the address-of operator (&) which is not valid here.
3fill in blank
hard

Complete the code to create a first-class callable from the static method MyClass::myMethod.

PHP
<?php
class MyClass {
    public static function myMethod() {
        return 'Hello';
    }
}

$callable = [1](...);
echo $callable();
?>
Drag options to blanks, or click blank then click option'
AMyClass::myMethod
B'MyClass::myMethod'
C'MyClass::myMethod()'
DMyClass::myMethod()
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes creates an old-style string callable.
Adding parentheses calls the method immediately instead of creating a callable.
4fill in blank
hard

Fill both blanks to create a first-class callable from an instance method myMethod of $obj.

PHP
<?php
class MyClass {
    public function myMethod() {
        return 'Hi';
    }
}
$obj = new MyClass();
$callable = [1]->[2](...);
echo $callable();
?>
Drag options to blanks, or click blank then click option'
A$obj
B'myMethod'
CmyMethod
D'MyClass::myMethod'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the method name (use bare identifier).
Using the class name instead of the object variable.
5fill in blank
hard

Fill all three blanks to create a first-class callable from a closure stored in $closure and call it with argument 'world'.

PHP
<?php
$closure = fn($name) => "Hello, $name!";
$callable = [1];
echo $callable([2]);

// Using first-class callable syntax
$callable2 = [3];
echo $callable2('world');
?>
Drag options to blanks, or click blank then click option'
A$closure
B'world'
C$closure(...)
Dclosure
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to quote the closure variable name.
Not using quotes for the string argument.
Forgetting the spread operator (...) for first-class syntax.