Closure binding

A closure in PHP is an object, and that object carries two things a plain function does not: captured variables and a class scope. The blocks show both, from a factor taken by use to a private property reached through bindTo().

Capture

A closure can capture nearby variables with use. The list is fixed at creation time, so later changes outside stay unseen unless captured by reference.

Capture keeps helpers short, because settings travel with the function instead of passing through every call.

<?php $factor = 3; $mul = function (int $n) use ($factor): int { return $n * $factor; }; echo $mul(10), PHP_EOL; $factor = 5; echo $mul(10), PHP_EOL; $byRef = function (int $n) use (&$factor): int { return $n * $factor; }; echo $byRef(10), PHP_EOL; $factor = 2; echo $byRef(10), PHP_EOL;

php capture.php

30
30
50
20

The first closure kept the 3 it saw at creation even after the variable changed, while the by-reference one followed every new value.

Bind with bindTo

A closure defined outside a class has no object context at first. Binding it to an object gives it a working this value.

The second argument sets the scope, which controls access to private and protected members.

<?php class Box { private int $value = 42; } $get = function (): int { return $this->value; }; $box = new Box(); $bound = $get->bindTo($box, Box::class); echo $bound(), PHP_EOL; try { $noScope = $get->bindTo($box); echo $noScope(), PHP_EOL; } catch (Error $e) { echo get_class($e), ": ", $e->getMessage(), PHP_EOL; }

php bind.php

42
Error: Cannot access private property Box::$value

The scope argument made the private property visible. Without it the closure still ran on the object but had no right to the private member.

Closure::call

Call is a shortcut for one time binding and running. It attaches the closure, calls it with the given arguments, and returns the result.

This form fits tests and debug snippets where a permanent bound copy would only add noise.

<?php class Counter { private int $count = 7; } $read = function (): int { return $this->count; }; echo $read->call(new Counter()), PHP_EOL;

php call.php

7

One call bound the closure to the object, reached the private property and returned the value, with no stored copy left behind.

Change scope

Scope decides which private members the closure may see. Binding the same function with another class name changes the visible internals.

Keep this trick in helpers and tests, since reaching around visibility can hide design issues in main code.

<?php class Secret { private string $code = "ABC"; } class Other { private string $code = "XYZ"; } $peek = function (): string { return $this->code; }; $onSecret = $peek->bindTo(new Secret(), Secret::class); echo $onSecret(), PHP_EOL; $onOther = $peek->bindTo(new Other(), Other::class); echo $onOther(), PHP_EOL; try { $wrong = $peek->bindTo(new Secret(), Other::class); echo $wrong(), PHP_EOL; } catch (Error $e) { echo get_class($e), ": ", $e->getMessage(), PHP_EOL; }

php scope.php

ABC
XYZ
Error: Cannot access private property Secret::$code

The same function read two different private properties because the scope changed, and the last call paired the scope of one class with the object of the other.

Exercises

  1. Capture a prefix by value and by reference and print both results after changing the prefix.
  2. Bind a closure to an object with and without the scope argument and compare the two results.
  3. Use call() to read a private property in one expression.
  4. Bind one closure to two objects of different classes and print what each scope sees.
  5. Explain why reaching a private property this way is a tool for tests rather than for production code.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does use ($factor) capture at the time the closure is created?

    Answer

    A copy of the current value

    A by-value capture copies the value once, so later changes outside stay invisible.

  2. What does use (&$factor) capture?

    Answer

    A reference, so later changes are visible inside

    The ampersand makes the closure share the variable, so it reads the current value each time.

  3. What does the second argument of bindTo() set?

    Answer

    The class scope, which decides which private members are visible

    The scope names the class whose private and protected members the closure may reach.

  4. What does Closure::call($object) do?

    Answer

    It binds for one call, runs it and returns the result

    call() is the one-time form, so no bound copy stays after the call.

  5. What does this program print? <?php $factor = 3; $mul = function (int $n) use ($factor): int { return $n * $factor; }; $factor = 5; echo $mul(10);

    Answer

    30

    The closure captured 3 at creation, so the later change does not reach it.

  6. What does this program print? <?php $factor = 3; $mul = function (int $n) use (&$factor): int { return $n * $factor; }; $factor = 5; echo $mul(10);

    Answer

    50

    The reference makes the closure read the value 5 that the variable holds at call time.

  7. What does this program print? <?php class Box { private int $value = 42; } $get = function (): int { return $this->value; }; $box = new Box(); $bound = $get->bindTo($box, Box::class); echo $bound();

    Answer

    42

    The scope argument Box::class gives the closure the right to the private property.

  8. What does this program print? <?php class Box { private int $value = 42; } $get = function (): int { return $this->value; }; try { $noScope = $get->bindTo(new Box()); echo $noScope(); } catch (Error $e) { echo get_class($e); }

    Answer

    Error

    Without a scope the closure runs on the object but has no right to the private member, so PHP throws an Error.

  9. What does this program print? <?php class Counter { private int $count = 7; } $read = function (): int { return $this->count; }; echo $read->call(new Counter());

    Answer

    7

    call() binds the closure to the new object with that object scope and returns the private value.

  10. Why keep closure binding out of ordinary production code?

    Answer

    Because it reaches around visibility and can hide design problems

    A closure that bypasses private members is a shortcut for tests, not a design for daily code.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev