Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to bind the closure to the object and call it.
PHP
<?php class Person { public $name = 'Alice'; } $person = new Person(); $greet = function() { return "Hello, " . $this->name; }; $boundGreet = $greet->[1]($person); echo $boundGreet(); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using call() instead of bindTo() causes errors because call() immediately invokes the closure.
Using apply() or attach() which do not exist in PHP for closures.
✗ Incorrect
The bindTo method binds a closure to an object, allowing it to use the object's properties via $this.
2fill in blank
mediumComplete the code to bind the closure to the object and specify the class scope.
PHP
<?php class User { private $role = 'admin'; public function getRoleClosure() { return function() { return $this->role; }->[1]($this, __CLASS__); } } $user = new User(); $closure = $user->getRoleClosure(); echo $closure(); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not passing the class scope causes private properties to be inaccessible.
Using call() which immediately invokes the closure instead of binding.
✗ Incorrect
bindTo can take a second argument to specify the class scope, allowing access to private properties.
3fill in blank
hardFix the error by completing the code to bind the closure to the object correctly.
PHP
<?php class Product { public $price = 100; } $product = new Product(); $discount = function($amount) { return $this->price - $amount; }; $boundDiscount = $discount->[1]($product); echo $boundDiscount(20); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using call() which immediately invokes the closure and does not return a bound closure.
Using non-existent methods like apply() or attach().
✗ Incorrect
bindTo binds the closure to the object so $this refers to the object inside the closure.
4fill in blank
hardFill both blanks to create a closure bound to the object and call it with an argument.
PHP
<?php class Calculator { public $factor = 2; } $calc = new Calculator(); $multiply = function($num) { return $num * $this->factor; }; $boundMultiply = $multiply->[1]([2]); echo $boundMultiply(5); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using call instead of bindTo returns the result immediately, not a bound closure.
Passing the closure itself instead of the object to bind.
✗ Incorrect
bindTo binds the closure to the object $calc so $this inside the closure refers to $calc.
5fill in blank
hardFill all three blanks to bind the closure to the object with class scope and call it with an argument.
PHP
<?php class Logger { private $prefix = '[LOG]'; } $logger = new Logger(); $logMessage = function($msg) { return $this->prefix . ' ' . $msg; }; $boundLog = $logMessage->[1]([2], [3]); echo $boundLog('System started'); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using call instead of bindTo causes immediate invocation, not binding.
Not passing the class scope causes private properties to be inaccessible.
✗ Incorrect
bindTo binds the closure to the object $logger with the class scope __CLASS__ to access private properties.