Bird
Raised Fist0
PHPprogramming~20 mins

Binding closures to objects in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Closure Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code using closure binding?
Consider the following PHP code. What will it output when run?
PHP
<?php
class Person {
    private string $name = 'Alice';
}

$closure = function() {
    return $this->name;
};

$person = new Person();
$boundClosure = $closure->bindTo($person, Person::class);
echo $boundClosure();
?>
ANULL
BFatal error: Uncaught Error: Cannot access private property Person::$name
CAlice
DError: Closure binding failed
Attempts:
2 left
💡 Hint
The closure is bound to the Person object with the correct scope.
❓ Predict Output
intermediate
2:00remaining
What error does this PHP closure binding code produce?
What error will this code produce when executed?
PHP
<?php
class Animal {
    private string $type = 'Cat';
}

$closure = function() {
    return $this->type;
};

$animal = new Animal();
$boundClosure = $closure->bindTo($animal, 'stdClass');
echo $boundClosure();
?>
AError: Closure binding failed
BCat
CNULL
DFatal error: Uncaught Error: Cannot access private property Animal::$type
Attempts:
2 left
💡 Hint
The closure is bound with a different class scope than the object's class.
🔧 Debug
advanced
2:00remaining
Why does this closure binding code cause an error?
This PHP code tries to bind a closure to an object but causes an error. What is the cause?
PHP
<?php
class Car {
    private string $model = 'Tesla';
}

$closure = function() {
    return $this->model;
};

$car = new Car();
$boundClosure = $closure->bindTo(null, Car::class);
echo $boundClosure();
?>
AFatal error because binding to null means no object context for $this
BOutputs 'Tesla' because scope is Car::class
COutputs NULL because no object is bound
DSyntax error due to missing semicolon
Attempts:
2 left
💡 Hint
Binding to null removes the object context, so $this is undefined.
❓ Predict Output
advanced
2:00remaining
What is the output of this closure bound to a subclass object?
What will this PHP code output?
PHP
<?php
class Vehicle {
    protected string $type = 'Vehicle';
}

class Bike extends Vehicle {
    private string $type = 'Bike';
}

$closure = function() {
    return $this->type;
};

$bike = new Bike();
$boundClosure = $closure->bindTo($bike, Bike::class);
echo $boundClosure();
?>
ABike
BVehicle
CFatal error: Cannot access private property Bike::$type
DNULL
Attempts:
2 left
💡 Hint
The closure is bound with Bike class scope, so it accesses Bike's private property.
🧠 Conceptual
expert
2:00remaining
How many items are in the resulting array after binding and invoking closures?
Given this PHP code, how many elements does the resulting array contain?
PHP
<?php
class Box {
    private int $count = 3;
}

$closure = function() {
    return [$this->count, $this->count * 2];
};

$box = new Box();
$boundClosure = $closure->bindTo($box, Box::class);
$result = $boundClosure();
print_r($result);
?>
A1
B2
C0
DFatal error
Attempts:
2 left
💡 Hint
The closure returns an array with two elements.

Practice

(1/5)
1. What does binding a closure to an object in PHP allow the closure to do?
easy
A. Change the object's class permanently
B. Access the object's properties and methods as if it were part of the object
C. Convert the closure into a regular function
D. Prevent the closure from accessing any object data

Solution

  1. Step 1: Understand closure binding concept

    Binding a closure to an object lets the closure use the object's properties and methods.
  2. 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.
  3. Final Answer:

    Access the object's properties and methods as if it were part of the object -> Option B
  4. Quick Check:

    Binding closure = access object data [OK]
Hint: Binding closures lets them use object data like methods [OK]
Common Mistakes:
  • Thinking binding changes the object's class
  • Believing closure becomes a normal function
  • Assuming binding blocks access to object data
2. Which of the following is the correct syntax to bind a closure $func to an object $obj in PHP?
easy
A. bindTo($func, $obj);
B. $func->bind($obj);
C. $func->bindTo($obj);
D. $obj->bindClosure($func);

Solution

  1. Step 1: Recall the method name for binding closures

    The method to bind a closure to an object is bindTo called on the closure.
  2. Step 2: Check syntax correctness

    The correct syntax is $func->bindTo($obj); where $func is the closure and $obj is the object.
  3. Final Answer:

    $func->bindTo($obj); -> Option C
  4. Quick Check:

    bindTo method on closure = correct syntax [OK]
Hint: Use bindTo() method on closure to bind object [OK]
Common Mistakes:
  • Using bind() instead of bindTo()
  • Calling bindTo as a global function
  • Trying to bind from the object instead of closure
3. What will be the output of the following PHP code?
$obj = new class { private $x = 10; };
$func = function() { return $this->x; };
$bound = $func->bindTo($obj, get_class($obj));
echo $bound();
medium
A. Error: Cannot access private property
B. Closure object
C. null
D. 10

Solution

  1. Step 1: Understand closure binding with private scope

    The closure is bound to $obj with the class scope of $obj, allowing access to private properties.
  2. Step 2: Evaluate the closure call

    Calling $bound() returns $this->x which is 10, accessible due to binding.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Binding with class scope allows private access = 10 [OK]
Hint: Binding with class scope grants private property access [OK]
Common Mistakes:
  • Ignoring class scope in bindTo causing access error
  • Expecting null because property is private
  • Thinking closure returns itself
4. Identify the error in this PHP code snippet:
$obj = new stdClass();
$func = function() { return $this->value; };
$bound = $func->bindTo($obj);
echo $bound();
medium
A. No error, outputs null
B. Fatal error: Cannot access property 'value' on stdClass
C. Error: Closure not bound to any object
D. Syntax error in closure definition

Solution

  1. Step 1: Check if binding is correct

    The closure is bound to $obj which is an instance of stdClass.
  2. Step 2: Check property existence and output

    $obj has no value property, so accessing it returns null without error.
  3. Final Answer:

    No error, outputs null -> Option A
  4. Quick Check:

    Accessing undefined property on stdClass = null [OK]
Hint: Undefined properties on stdClass return null, no error [OK]
Common Mistakes:
  • Expecting fatal error for missing property
  • Thinking closure is unbound
  • Assuming syntax error in closure
5. Given the class below, how can you use a closure bound to an instance to safely increment the private counter property?
class Counter {
private int $counter = 0;
public function get() { return $this->counter; }
}

Choose the correct closure binding and usage.
hard
A. $inc = function() { $this->counter++; }; $inc = $inc->bindTo($obj, Counter::class); $inc();
B. $inc = function() { $this->counter++; }; $inc();
C. $inc = function() { $this->counter++; }; $inc = $inc->bindTo($obj); $inc();
D. $inc = function() { $this->counter++; }; $inc = $inc->bindTo(null, Counter::class); $inc();

Solution

  1. Step 1: Understand private property access

    To access private counter, closure must be bound to object $obj with class scope Counter::class.
  2. Step 2: Check binding and invocation

    $inc = function() { $this->counter++; }; $inc = $inc->bindTo($obj, Counter::class); $inc(); binds closure to $obj and class Counter, then calls it to increment counter.
  3. Final Answer:

    $inc = function() { $this->counter++; }; $inc = $inc->bindTo($obj, Counter::class); $inc(); -> Option A
  4. Quick Check:

    Bind closure to object and class for private access [OK]
Hint: Bind closure with object and class to access private properties [OK]
Common Mistakes:
  • Not specifying class scope in bindTo
  • Calling closure without binding
  • Binding with null object or missing object
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