Challenge - 5 Problems
Closure Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); ?>
Attempts:
2 left
💡 Hint
The closure is bound to the Person object with the correct scope.
✗ Incorrect
The closure is bound to the Person object and its class scope, so it can access the private property $name. It returns 'Alice'.
❓ Predict Output
intermediate2: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(); ?>
Attempts:
2 left
💡 Hint
The closure is bound with a different class scope than the object's class.
✗ Incorrect
Binding the closure to the Animal object but with 'stdClass' scope means it cannot access Animal's private properties, causing a fatal error.
🔧 Debug
advanced2: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(); ?>
Attempts:
2 left
💡 Hint
Binding to null removes the object context, so $this is undefined.
✗ Incorrect
Binding the closure to null removes the object context, so inside the closure $this is not defined, causing a fatal error when accessing $this->model.
❓ Predict Output
advanced2: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(); ?>
Attempts:
2 left
💡 Hint
The closure is bound with Bike class scope, so it accesses Bike's private property.
✗ Incorrect
Binding the closure to the Bike object with Bike class scope allows access to Bike's private $type property, which is 'Bike'.
🧠 Conceptual
expert2: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); ?>
Attempts:
2 left
💡 Hint
The closure returns an array with two elements.
✗ Incorrect
The closure returns an array with two elements: the private property count (3) and its double (6). So the array has 2 elements.