Understanding PHP Closures with a Simple Real-Life Example

When you’re learning PHP, one confusing thing that often pops up is the term “Closure”. Any anonymous fn() is always an object of default php Closure class.

If you’ve ever written an anonymous function like this:

$fun = function () {
    return 'Do fun!';
};

You might think this is just a function. But behind the scenes, PHP treats it as a Closure object. Sounds complicated? Don’t worry — this post will explain it in the simplest way possible with an easy-to-understand example.


🧠 What is a Closure in PHP?

A Closure is just a fancy name for an anonymous function object. In PHP:

  • When you create an anonymous function (a function without a name), PHP automatically treats it as an object of the Closure class.
  • You can store it in a variable, pass it to another function, and call it later.

Let’s look at an example:

$fun = function ($money) {
    return 'Do fun with $' . $money;
};

Here, $fun is a variable holding an anonymous function, and it is also an object of the Closure class.

You can check it:

var_dump($fun);

🖨️ Output:

object(Closure)#1 (0) {
}

😕 Why echo $fun; Doesn’t Work

If you try this:

echo $fun;

You’ll get this error:

Uncaught Error: Object of class Closure could not be converted to string

Why? Because $fun is not a string — it’s an object. PHP can’t automatically convert a Closure object into a string.

✅ To make it work, you must call the function:

echo $fun(100); // Output: Do fun with $100

🎉 Let’s Build a Simple Real-Life Example

Let’s say you have a joy function that takes two numbers and a callback function. It multiplies the two numbers and then gives the result to the callback.

$joy = function ($a, $b, Closure $callback) {
    $m = $a * $b;
    return $callback($m);
};

Now define two different callback functions:

$fun = function ($money) {
    return 'Do fun with $' . $money;
};

$penalty = function ($money) {
    return 'Pay fine of $' . $money;
};

You can now pass these anonymous functions to $joy:

echo $joy(20, 5, $fun);     // Output: Do fun with $100
echo $joy(20, 5, $penalty); // Output: Pay fine of $100

What’s happening here?

  1. $joy(20, 5, $fun) → multiplies 20 × 5 = 100
  2. Then it calls the $fun(100) function
  3. Returns: “Do fun with $100”

✅ Key Points to Remember

  • An anonymous function is automatically treated as a Closure object in PHP.
  • ✅ You can’t echo a Closure object directly — it’s not a string.
  • ✅ You can call the closure like a normal function: $fun(100)
  • ✅ You can pass closures as arguments to other functions — like in Laravel middleware or callback systems.

🧪 Final Example (All Together)

$fun = function ($money) {
    return 'Do fun with $' . $money;
};

$penalty = function ($money) {
    return 'Pay fine of $' . $money;
};

$joy = function ($a, $b, Closure $callback) {
    $m = $a * $b;
    return $callback($m);
};

echo $joy(20, 5, $fun);     // Do fun with $100
echo "\n";
echo $joy(10, 10, $penalty); // Pay fine of $100

🚀 Conclusion

Closures might sound complicated at first, but they are simply anonymous functions that can be passed around and executed like any other value. Once you understand that they’re just functions stored in variables, it becomes much easier to use them — especially in frameworks like Laravel.

Let me know if you want to see how Laravel middleware uses closures behind the scenes in a similar way! 😊