Closures let you create small pieces of code that you can pass around and run later. Using them as callbacks means you can tell a function what to do at a certain time.
Closures as callbacks in PHP
<?php function exampleFunction(callable $callback) { // Do something $callback(); } exampleFunction(function() { echo "Hello from closure callback!"; }); ?>
A closure is an anonymous function you can create without a name.
Callbacks are functions passed as arguments to other functions to be called later.
<?php // Simple closure as callback array_map(function($n) { return $n * 2; }, [1, 2, 3]); ?>
<?php // Closure with use keyword to access outside variable $multiplier = 3; array_map(function($n) use ($multiplier) { return $n * $multiplier; }, [1, 2, 3]); ?>
<?php // Passing closure as callback to a custom function function greet(callable $callback) { $callback("Alice"); } greet(function($name) { echo "Hello, $name!"; }); ?>
This program uses a closure as a callback to double each number in the list and print it.
<?php function processNumbers(array $numbers, callable $callback) { foreach ($numbers as $number) { echo $callback($number) . "\n"; } } // Closure doubles the number processNumbers([1, 2, 3], function($n) { return $n * 2; }); ?>
Closures can capture variables from outside their scope using the use keyword.
Callbacks must be callable, which means they can be a closure, a named function, or an array with object and method.
Using closures as callbacks helps keep code short and focused.
Closures are anonymous functions you can pass as callbacks.
Callbacks let you customize behavior by passing code to other functions.
Closures can access outside variables with use.
Practice
Solution
Step 1: Understand what a closure is
A closure is an anonymous function, meaning it has no name and can be created on the fly.Step 2: Recognize closures as callbacks
Closures can be passed as arguments to other functions to customize behavior, making them useful as callbacks.Final Answer:
An anonymous function that can be passed as an argument -> Option CQuick Check:
Closure = anonymous function callback [OK]
- Thinking closures must have names
- Confusing closures with variables
- Believing closures cannot be passed as arguments
Solution
Step 1: Check closure syntax
The correct syntax for an anonymous function uses the keywordfunctionfollowed by parentheses with parameters, then braces with the function body.Step 2: Validate the return statement
The closure must explicitly return the value to be used byarray_map. array_map(function($x) { return $x * 2; }, [1, 2, 3]); correctly returns$x * 2.Final Answer:
array_map(function($x) { return $x * 2; }, [1, 2, 3]); -> Option DQuick Check:
Correct closure syntax = array_map(function($x) { return $x * 2; }, [1, 2, 3]); [OK]
- Omitting parentheses around parameters
- Missing return keyword inside closure
- Using arrow (=>) incorrectly in closure
$numbers = [1, 2, 3];
$result = array_map(function($n) { return $n + 5; }, $numbers);
print_r($result);Solution
Step 1: Understand array_map with closure
The closure adds 5 to each element of the array$numbers.Step 2: Calculate each element
1+5=6, 2+5=7, 3+5=8, so the resulting array is [6, 7, 8].Final Answer:
[6, 7, 8] -> Option BQuick Check:
Each element + 5 = [6,7,8] [OK]
- Confusing original array with result
- Forgetting closure return value
- Misreading array_map behavior
$multiplier = 3;
$numbers = [1, 2, 3];
$result = array_map(function($n) { return $n * $multiplier; }, $numbers);
print_r($result);Solution
Step 1: Check variable scope inside closure
Variables outside a closure are not accessible inside unless passed withuse.Step 2: Identify missing
The closure needsusekeyworduse ($multiplier)to access the external variable.Final Answer:
The variable $multiplier is not accessible inside the closure -> Option AQuick Check:
External vars need use() in closures [OK]
- Assuming closures access all outside variables automatically
- Thinking closures must be named to access variables
- Believing array_map rejects closures
$factor = 4;
$nums = [2, 4, 6];
$results = array_map(function($num) { return $num * $factor; }, $nums);
print_r($results);Solution
Step 1: Understand variable scope in closures
To access an external variable inside a closure, you must explicitly import it usinguse.Step 2: Apply
Modify the closure touse ($factor)syntaxfunction($num) use ($factor) { return $num * $factor; }to access$factor.Final Answer:
Add use ($factor) after the function parentheses -> Option AQuick Check:
use() imports external vars into closures [OK]
- Trying to access external variables without use()
- Using global keyword inside closures incorrectly
- Passing extra arguments to array_map
Ready to prove it?
Pass the test and this pattern moves unknown
