0
0
PHPprogramming~10 mins

First-class callable syntax in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - First-class callable syntax
Define function
Create callable using first-class syntax
Store callable in variable
Call the callable variable
Execute function body
Return result
This flow shows how a function is defined, converted to a callable using first-class syntax, stored, and then called to execute.
Execution Sample
PHP
<?php
function greet($name) {
    return "Hello, $name!";
}

$greeter = greet(...);
echo $greeter("Alice");
Defines a function greet, creates a first-class callable from it, stores it in a variable, and calls it with "Alice".
Execution Table
StepActionCode LineVariable StateOutput
1Define function greetfunction greet($name) {...}No variables yet
2Create first-class callable from greet$greeter = greet(...);$greeter = callable to greet function
3Call callable stored in $greeter with "Alice"echo $greeter("Alice");$greeter callable activeHello, Alice!
4Program endsEnd of scriptNo change
💡 Script ends after outputting the greeting.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
$greeterundefinedcallable to greet functioncallable to greet functioncallable to greet function
Key Moments - 2 Insights
Why do we use greet(...) instead of just greet when assigning to $greeter?
Using greet(...) creates a first-class callable, which is a callable version of the function. Just using greet would assign the string 'greet' (which is also callable but not first-class).
What happens when we call $greeter("Alice")?
The callable stored in $greeter runs the greet function with "Alice" as argument, producing the output "Hello, Alice!" as shown in step 3 of the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $greeter after step 2?
AA string "greet"
BA callable to the greet function
CNull
DAn error
💡 Hint
Check the 'Variable State' column at step 2 in the execution table.
At which step does the program output "Hello, Alice!"?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Output' column in the execution table.
If we replaced greet(...) with just greet in the assignment, what would happen?
ASyntax error
BThe function would be called immediately and $greeter would hold the return value
C$greeter would hold a callable
DNothing changes
💡 Hint
Refer to the key moment about why greet(...) is used instead of greet.
Concept Snapshot
First-class callable syntax in PHP:
- Use functionName(...) to get a callable.
- Assign callable to variable: $var = functionName(...);
- Call via variable: $var(args);
- Allows passing functions as values easily.
Full Transcript
This example shows how to use PHP's first-class callable syntax. First, a function greet is defined. Then, using greet(...), a callable version of greet is created and stored in $greeter. When $greeter is called with "Alice", it runs greet("Alice") and outputs "Hello, Alice!". This syntax helps treat functions as values, making code flexible and clear.