It is a way to create a callable reference to a function or method using the syntax foo(...) or ClassName::methodName(...) that can be passed around like a variable.
Click to reveal answer
beginner
How do you create a first-class callable for a named function <code>myFunction</code>?
Use $callable = myFunction(...); to get a callable reference to myFunction.
Click to reveal answer
intermediate
How can you create a first-class callable for a static method <code>MyClass::myStaticMethod</code>?
Use $callable = MyClass::myStaticMethod(...); to get a callable reference to the static method.
Click to reveal answer
intermediate
What is the benefit of using first-class callable syntax over traditional callables in PHP?
It is simpler and more readable. You can create callables without using strings or arrays, reducing errors and improving code clarity.
Click to reveal answer
advanced
Can first-class callable syntax be used with closures (anonymous functions)?
No, first-class callable syntax is for named functions and methods. Closures are already callable objects and do not need this syntax.
Click to reveal answer
Which syntax creates a first-class callable for a function named process?
Aprocess(...)
Bprocess()
C"process"
D["process"]
✗ Incorrect
The syntax process(...) creates a first-class callable reference to the function process.
How do you create a first-class callable for a static method Logger::log?
ALogger::log(...)
BLogger::log()
C[Logger, 'log']
D"Logger::log"
✗ Incorrect
Using Logger::log(...) creates a first-class callable for the static method.
What does the ... (splat operator) do in first-class callable syntax?
ACalls the function immediately
BIndicates a callable reference
CCreates an array
DUnpacks arguments
✗ Incorrect
In this context, ... after the function name indicates creating a callable reference, not calling it.
Can you use first-class callable syntax with anonymous functions (closures)?
AYes, but only with named closures
BYes, with <code>closure(...)</code>
CNo, closures cannot be called
DNo, closures are already callable objects
✗ Incorrect
Closures are already callable objects, so first-class callable syntax is not needed for them.
What is a key advantage of first-class callable syntax in PHP?
AAutomatically executes functions
BReplaces all function calls
CSimplifies creating callable references
DOnly works with static methods
✗ Incorrect
It simplifies creating callable references, making code easier to read and less error-prone.
Explain how to create a first-class callable for a named function and a static method in PHP.
Think about how you write the function or method name followed by three dots.
You got /3 concepts.
Describe the benefits of using first-class callable syntax compared to traditional callables in PHP.
Focus on how the new syntax makes code easier to write and understand.
You got /3 concepts.
Practice
(1/5)
1. What does the first-class callable syntax in PHP allow you to do?
easy
A. Automatically convert functions to strings
B. Create new functions dynamically at runtime
C. Prevent functions from being called more than once
D. Treat functions as variables to pass or store them easily
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 D
Quick Check:
First-class callable = Treat functions as variables [OK]
Hint: Remember: first-class means functions act like variables [OK]
Common Mistakes:
Confusing callable syntax with dynamic function creation
Thinking it converts functions to strings
Assuming it restricts function calls
2. Which of the following is the correct syntax to get a first-class callable for a function named foo in PHP?
easy
A. $callable = foo(...);
B. $callable = foo();
C. $callable = foo\(...);
D. $callable = foo;
Solution
Step 1: Recall the first-class callable syntax
In PHP, to get a callable for a function, you use function_name(...).
Step 2: Match the syntax with options
$callable = foo(...); uses foo(...), 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 A
Quick Check:
Callable syntax = function_name(...) [OK]
Hint: Use parentheses with ellipsis to get callable: foo(...) [OK]
Common Mistakes:
Calling the function instead of getting callable
Omitting parentheses and ellipsis
Using backslash incorrectly for global functions
3. What will be the output of the following PHP code?
$fn = strlen(...);
echo $fn('hello');
medium
A. 5
B. hello
C. strlen
D. Error: Call to undefined function
Solution
Step 1: Understand the callable assignment
The variable $fn is assigned the callable for the built-in function strlen using first-class callable syntax.
Step 2: Call the callable with argument 'hello'
Calling $fn('hello') executes strlen('hello'), which returns 5.
Final Answer:
5 -> Option A
Quick Check:
strlen('hello') = 5 [OK]
Hint: Callable with strlen(...) returns string length [OK]
Common Mistakes:
Expecting the function name as output
Confusing callable with string
Thinking it causes an error
4. Identify the error in the following PHP code using first-class callable syntax: