Binding closures to objects in PHP - Time & Space Complexity
We want to understand how the time it takes to run code changes when we bind closures to objects in PHP.
Specifically, how does the number of operations grow as the number of bindings increases?
Analyze the time complexity of the following code snippet.
class MyClass {
public $value;
}
$closure = function() {
return $this->value;
};
$objects = [];
for ($i = 0; $i < $n; $i++) {
$obj = new MyClass();
$obj->value = $i;
$boundClosure = $closure->bindTo($obj, MyClass::class);
$objects[] = $boundClosure();
}
This code creates $n objects, binds the same closure to each object, and calls it to get a value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that creates objects, binds the closure, and calls it.
- How many times: Exactly
ntimes, once for each object.
Each iteration does a fixed amount of work: creating an object, binding the closure, and calling it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times the fixed work |
| 100 | About 100 times the fixed work |
| 1000 | About 1000 times the fixed work |
Pattern observation: The total work grows directly in proportion to n.
Time Complexity: O(n)
This means the time to run the code grows linearly as the number of objects increases.
[X] Wrong: "Binding a closure to an object is instant and does not add to the time cost."
[OK] Correct: Binding actually creates a new closure context each time, so doing it repeatedly adds time proportional to how many times you do it.
Understanding how binding closures affects performance helps you write efficient PHP code and shows you can think about how code scales with input size.
"What if we bind the closure once outside the loop and reuse it for all objects? How would the time complexity change?"
Practice
Solution
Step 1: Understand closure binding concept
Binding a closure to an object lets the closure use the object's properties and methods.Step 2: Identify what binding does
It does not change the object's class or convert the closure; it only changes the closure's context.Final Answer:
Access the object's properties and methods as if it were part of the object -> Option BQuick Check:
Binding closure = access object data [OK]
- Thinking binding changes the object's class
- Believing closure becomes a normal function
- Assuming binding blocks access to object data
$func to an object $obj in PHP?Solution
Step 1: Recall the method name for binding closures
The method to bind a closure to an object isbindTocalled on the closure.Step 2: Check syntax correctness
The correct syntax is$func->bindTo($obj);where$funcis the closure and$objis the object.Final Answer:
$func->bindTo($obj); -> Option CQuick Check:
bindTo method on closure = correct syntax [OK]
- Using bind() instead of bindTo()
- Calling bindTo as a global function
- Trying to bind from the object instead of closure
$obj = new class { private $x = 10; };
$func = function() { return $this->x; };
$bound = $func->bindTo($obj, get_class($obj));
echo $bound();Solution
Step 1: Understand closure binding with private scope
The closure is bound to$objwith the class scope of$obj, allowing access to private properties.Step 2: Evaluate the closure call
Calling$bound()returns$this->xwhich is 10, accessible due to binding.Final Answer:
10 -> Option DQuick Check:
Binding with class scope allows private access = 10 [OK]
- Ignoring class scope in bindTo causing access error
- Expecting null because property is private
- Thinking closure returns itself
$obj = new stdClass();
$func = function() { return $this->value; };
$bound = $func->bindTo($obj);
echo $bound();
Solution
Step 1: Check if binding is correct
The closure is bound to$objwhich is an instance of stdClass.Step 2: Check property existence and output
$objhas novalueproperty, so accessing it returns null without error.Final Answer:
No error, outputs null -> Option AQuick Check:
Accessing undefined property on stdClass = null [OK]
- Expecting fatal error for missing property
- Thinking closure is unbound
- Assuming syntax error in closure
counter property?class Counter {
private int $counter = 0;
public function get() { return $this->counter; }
}Choose the correct closure binding and usage.
Solution
Step 1: Understand private property access
To access privatecounter, closure must be bound to object$objwith class scopeCounter::class.Step 2: Check binding and invocation
$inc = function() { $this->counter++; }; $inc = $inc->bindTo($obj, Counter::class); $inc(); binds closure to$objand classCounter, then calls it to incrementcounter.Final Answer:
$inc = function() { $this->counter++; }; $inc = $inc->bindTo($obj, Counter::class); $inc(); -> Option AQuick Check:
Bind closure to object and class for private access [OK]
- Not specifying class scope in bindTo
- Calling closure without binding
- Binding with null object or missing object
Ready to prove it?
Pass the test and this pattern moves unknown
