0
0
PHPprogramming~10 mins

__invoke for callable objects in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - __invoke for callable objects
Create object of class with __invoke
Call object as function
PHP calls __invoke method
Execute __invoke code
Return or print result
End
When you call an object like a function, PHP runs its __invoke method automatically.
Execution Sample
PHP
<?php
class Greeter {
  public function __invoke($name) {
    return "Hello, $name!";
  }
}
$greet = new Greeter();
echo $greet("Alice");
This code creates a Greeter object and calls it like a function to greet Alice.
Execution Table
StepActionEvaluationResult
1Create Greeter objectnew Greeter()Object $greet created
2Call $greet("Alice")Calls __invoke with 'Alice'Executes __invoke method
3__invoke method runsreturn "Hello, Alice!"Returns string 'Hello, Alice!'
4echo outputPrints returned stringOutput: Hello, Alice!
💡 Execution ends after printing the greeting.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$greetundefinedGreeter objectGreeter objectGreeter objectGreeter object
Input to __invokenonenone"Alice""Alice""Alice"
Return valuenonenonenone"Hello, Alice!""Hello, Alice!"
Key Moments - 3 Insights
Why can we call an object like a function?
Because the class has a __invoke method, PHP lets you use the object as if it were a function. See step 2 in execution_table.
What happens if __invoke is not defined but we call the object?
PHP will give an error because it doesn't know what to do when the object is called like a function. This is why __invoke must exist to allow this.
Does __invoke have to return a value?
No, but if you want to get a result from calling the object, __invoke should return something. In the example, it returns a greeting string (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result returned by __invoke at step 3?
AGreeter object
B"Alice"
C"Hello, Alice!"
DNo return value
💡 Hint
Check the 'Result' column at step 3 in execution_table.
At which step does PHP automatically call the __invoke method?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column describing the call to $greet("Alice") in execution_table.
If the __invoke method did not return anything, what would echo print at step 4?
AAn error message
BNothing (empty output)
C"Hello, Alice!"
DThe object address
💡 Hint
Refer to the 'Return value' in variable_tracker and what echo prints in execution_table step 4.
Concept Snapshot
__invoke method lets you call an object like a function.
Define __invoke inside a class to handle calls.
When you do $obj(), PHP runs $obj->__invoke().
You can pass arguments and return values.
Useful for callable objects and cleaner code.
Full Transcript
This example shows how PHP's __invoke method works. First, an object of class Greeter is created. Then, when we call the object like a function with $greet("Alice"), PHP automatically calls the __invoke method inside Greeter with the argument "Alice". The __invoke method returns the string "Hello, Alice!" which is then printed by echo. Variables like $greet hold the object, and the input and return values flow through the __invoke method. If __invoke was missing, calling the object like a function would cause an error. This feature lets objects behave like functions for flexible and readable code.