0
0
PHPprogramming~20 mins

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

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