This article explains PHP closures, special functions capturing outside variables using the "use" keyword for persistent access.
In PHP, a closure provides a way to treat a function as a variable, allowing you to store it and execute it later. This flexibility is enhanced by the 'use' keyword, which enables closures to access variables from their surrounding scope, even after the code that defined them has finished running.
A closure in PHP is like a function you can store in a variable. Think of it as a function on-the-go.
$myClosure = function() {
echo "Hello from the closure!";
};
$myClosure(); // This calls the closure
The real power comes with the use
keyword. It lets the closure access variables from the surrounding code, even after that code has finished running.
$name = "Alice";
$greet = function() use ($name) {
echo "Hello, $name!";
};
$greet(); // Outputs "Hello, Alice!"
Here's why use
is important:
use
, a closure can't see variables from where it was defined.use
makes a copy: The closure gets its own version of the variable. Changing the original outside won't affect the closure's copy.$count = 10;
$increment = function() use ($count) {
$count++;
echo $count; // Outputs 11
};
$increment();
echo $count; // Still outputs 10
To modify the original variable inside the closure, pass it by reference using &$variable
within use
.
Closures are super handy for:
array_map
or usort
to customize their behavior.Keep in mind:
use
is for capturing variables when the closure is created.This PHP code demonstrates closures, showcasing their definition, execution, and how they interact with external variables using the 'use' keyword. It illustrates that 'use' by default creates a copy of a variable, and how to modify the original variable using pass-by-reference. The example with 'array_map' highlights the practical use of closures as callbacks in PHP.
This script demonstrates the concepts of closures, use
keyword, and passing by reference:
<?php
// Simple closure example
$myClosure = function() {
echo "Hello from the closure!\n";
};
$myClosure(); // Outputs: Hello from the closure!
// Closure with 'use' keyword
$name = "Alice";
$greet = function() use ($name) {
echo "Hello, $name!\n";
};
$greet(); // Outputs: Hello, Alice!
// Demonstrating closure scope and 'use' making a copy
$count = 10;
$increment = function() use ($count) {
$count++;
echo "Inside closure: $count\n"; // Outputs: Inside closure: 11
};
$increment();
echo "Outside closure: $count\n"; // Outputs: Outside closure: 10
// Modifying the original variable using pass-by-reference
$number = 5;
$doubleIt = function() use (&$number) {
$number *= 2;
};
$doubleIt();
echo "Doubled number: $number\n"; // Outputs: Doubled number: 10
// Using closures as callbacks with array_map
$numbers = [1, 2, 3, 4, 5];
$squaredNumbers = array_map(function($n) {
return $n * $n;
}, $numbers);
print_r($squaredNumbers); // Outputs: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
This code demonstrates:
use
to access external variables.use
creates a copy, not a reference.&$variable
in use
.array_map
.This provides a practical understanding of closures and their applications in PHP.
Closure
class, giving them object-oriented capabilities.Feature | Description | Example |
---|---|---|
What is a closure? | A function you can store in a variable. | $myClosure = function() { echo "Hello!"; }; |
Calling a closure | Use the variable name followed by parentheses. | $myClosure(); |
use keyword |
Lets closures access variables from their surrounding scope. | $greet = function() use ($name) { echo "Hello, $name!"; }; |
Closure scope | Closures have their own scope; use is needed to access external variables. |
|
use creates a copy |
Changes to the original variable outside the closure don't affect the closure's copy. | |
Passing by reference | Use &$variable within use to modify the original variable inside the closure. |
$increment = function() use (&$count) { $count++; }; |
Use cases | Callbacks (e.g., with array_map , usort ), creating small, focused functions. |
|
Closure parameters | Taken when the closure is called, separate from variables captured with use . |
|
PHP version | Closures were introduced in PHP 5.3. |
Closures in PHP offer a powerful way to handle functions as first-class citizens, enabling you to write cleaner, more modular, and more maintainable code. By understanding how to define closures, leverage the 'use' keyword for variable scoping, and apply closures in practical scenarios like callbacks, you can significantly enhance your PHP development skills. Remember that closures are available from PHP 5.3 onwards, so ensure compatibility if you're working with legacy codebases. As you explore closures further, you'll discover even more advanced techniques like binding closures to objects and using them for function currying, opening up a world of possibilities for writing expressive and efficient PHP code.