Kritim Yantra
May 18, 2025
PHP is a flexible and powerful language, and one of the features that makes it so dynamic is the Closure or Anonymous Function.
In this blog, you’ll learn:
use
keyword and variable bindingLet’s unravel this awesome feature! 🧠
Anonymous functions are functions without a name. They are most commonly used for:
In PHP, anonymous functions are implemented using the function
keyword without a function name.
$greet = function($name) {
return "Hello, $name!";
};
echo $greet("Ajay"); // Hello, Ajay!
A Closure in PHP is an anonymous function that can capture variables from its surrounding scope using the use
keyword.
$message = "Hi";
$sayHi = function() use ($message) {
echo $message;
};
$sayHi(); // Hi
Closures "close over" the variable
$message
, hence the name.
use
$name = "Krishna";
$greet = function($greeting) use ($name) {
return "$greeting, $name!";
};
echo $greet("Good Morning"); // Good Morning, Krishna!
If you change $name
after the closure is created, the closure still uses the value from the moment it was declared:
$name = "Krishna";
$greet = function() use ($name) {
echo $name;
};
$name = "Ramesh";
$greet(); // Krishna (not Ramesh)
If you want the closure to reflect changes, use
&
to pass by reference.
$name = "Krishna";
$greet = function() use (&$name) {
echo $name;
};
$name = "Ramesh";
$greet(); // Ramesh
Closures are often passed as arguments — a common example is with array functions.
$numbers = [1, 2, 3, 4];
$squared = array_map(function($num) {
return $num * $num;
}, $numbers);
print_r($squared); // [1, 4, 9, 16]
$users = [
["name" => "Ajay", "age" => 25],
["name" => "Rahul", "age" => 17],
["name" => "Meena", "age" => 30],
];
$adults = array_filter($users, function($user) {
return $user['age'] >= 18;
});
print_r($adults);
You can use closures inside classes — even bind them to $this
using Closure::bind
.
class User {
public $name = "Ajay";
public function getGreeter() {
return function() {
return "Hello, " . $this->name;
};
}
}
$user = new User();
$greeter = $user->getGreeter();
echo $greeter(); // Hello, Ajay
Closure::bind
class Person {
private $name = "Secret User";
}
$getName = function() {
return $this->name;
};
$bound = $getName->bindTo(new Person(), 'Person');
echo $bound(); // Secret User
This is helpful for advanced meta-programming or testing private data.
Yes! PHP supports higher-order functions.
function multiplier($n) {
return function($x) use ($n) {
return $x * $n;
};
}
$triple = multiplier(3);
echo $triple(10); // 30
Laravel developers use closures everywhere:
Route::get('/', function () {
return view('welcome');
});
use()
carefully — don't overbind variables you don’t need.Closures and anonymous functions are a powerful part of modern PHP. They enable dynamic behavior, functional-style programming, and help you write concise, elegant code — especially in frameworks like Laravel.
Whether you’re building APIs, working with collections, or setting up routes — closures are everywhere!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google