Bird
Raised Fist0
PHPprogramming~10 mins

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

Choose your learning style10 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.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Treat functions as variables to pass or store them easily -> Option D
  4. 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

  1. Step 1: Recall the first-class callable syntax

    In PHP, to get a callable for a function, you use function_name(...).
  2. 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.
  3. Final Answer:

    $callable = foo(...); -> Option A
  4. 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

  1. Step 1: Understand the callable assignment

    The variable $fn is assigned the callable for the built-in function strlen using first-class callable syntax.
  2. Step 2: Call the callable with argument 'hello'

    Calling $fn('hello') executes strlen('hello'), which returns 5.
  3. Final Answer:

    5 -> Option A
  4. 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:

function greet($name) {
    return "Hello, $name!";
}

$greet = greet;
echo $greet('World');
medium
A. No error, code runs fine
B. Missing parentheses and ellipsis to get callable
C. Cannot assign function to variable
D. Function greet is not defined

Solution

  1. Step 1: Check how callable is assigned

    The code assigns $greet = greet; which is invalid syntax for first-class callable.
  2. Step 2: Correct syntax requires parentheses and ellipsis

    To get a callable, it should be $greet = greet(...);. Without this, PHP treats it as a constant or causes error.
  3. Final Answer:

    Missing parentheses and ellipsis to get callable -> Option B
  4. Quick Check:

    Callable syntax needs (...) [OK]
Hint: Always use (...) to get callable, not just function name [OK]
Common Mistakes:
  • Assigning function name without parentheses
  • Assuming function name alone is callable
  • Ignoring syntax errors
5. Given the following PHP code, what will be the output?

function multiply($a, $b) {
    return $a * $b;
}

$operations = [
    'double' => multiply(...),
    'square' => fn($x) => multiply($x, $x),
];

echo $operations['double'](5, 2) . ', ' . $operations['square'](4);
hard
A. Error: Cannot use multiply(...) here
B. 7, 16
C. 10, 16
D. 10, 8

Solution

  1. Step 1: Understand the callable assignments

    The array $operations stores two callables: 'double' uses first-class callable multiply(...), 'square' uses an arrow function calling multiply.
  2. Step 2: Evaluate each callable call

    $operations['double'](5, 2) calls multiply(5, 2) = 10.
    $operations['square'](4) calls multiply(4, 4) = 16.
  3. Final Answer:

    10, 16 -> Option C
  4. Quick Check:

    multiply(5,2)=10 and multiply(4,4)=16 [OK]
Hint: First-class callable works inside arrays like normal functions [OK]
Common Mistakes:
  • Confusing arrow function with callable syntax
  • Expecting error from multiply(...) usage
  • Mixing up multiply arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard