0
0
PHPprogramming~5 mins

First-class callable syntax in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the first-class callable syntax in PHP?
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"]
How do you create a first-class callable for a static method Logger::log?
ALogger::log(...)
BLogger::log()
C[Logger, 'log']
D"Logger::log"
What does the ... (splat operator) do in first-class callable syntax?
ACalls the function immediately
BIndicates a callable reference
CCreates an array
DUnpacks arguments
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
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
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.