0
0
PHPprogramming~10 mins

Binding closures to objects in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Acall
BbindTo
Capply
Dattach
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.
2fill in blank
medium

Complete 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'
AbindTo
Bcall
Capply
Dattach
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.
3fill in blank
hard

Fix 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'
Aapply
Bcall
Cattach
DbindTo
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().
4fill in blank
hard

Fill 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'
AbindTo
Bcall
C$calc
D$multiply
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.
5fill in blank
hard

Fill 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'
AbindTo
B$logger
C__CLASS__
Dcall
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.