Sling Academy
Home/PHP/Closure in PHP: A Complete Guide

Closure in PHP: A Complete Guide

Last updated: January 09, 2024

Introduction

Closures, a concept often associated with functional programming, have become a powerful component in modern PHP development. This guide unpacks the mystery behind closures in PHP, demonstrating how they can be leveraged for cleaner and more modular code.

Understanding Closures

In PHP, a closure is an anonymous function that can capture variables from its surrounding scope. These functions allow you to encapsulate functionality and they have great synergy with callback patterns.

<?php
$value = 10;
$closure = function() use ($value) {
    return $value + 20;
};
echo $closure();  // Outputs: 30
?>

Variable Inheritance

Closures ‘inherit’ variables from the parent scope using the use keyword. This allows the function to maintain state in a controlled manner.

<?php
$incrementBy = 5;
$adder = function($num) use ($incrementBy) {
    return $num + $incrementBy;
};
echo $adder(10);  // Outputs: 15
?>

Changing External Variables

Since PHP 5.4, you can also pass variable by reference to a closure, allowing it to modify external variables.

<?php
$count = 0;
$increment = function() use (&$count) {
    $count++;
};
$increment();
echo $count;  // Outputs: 1
?>

Use in Built-in Functions

Closures are often passed as callbacks to PHP’s array functions like array_filter and array_map.

<?php
$numbers = [1, 2, 3, 4, 5];
$even = array_filter($numbers, function($num) {
    return $num % 2 === 0;
});
print_r($even);  // Outputs: Array ( [1] => 2 [3] => 4 )
?>

Advanced Closure Concepts

Using Callable Type

PHP supports type hinting for callables, allowing you to ensure that a variable is a valid function or closure.

<?php
function runCalculation(callable $operation, $num1, $num2) {
    return $operation($num1, $num2);
}
$result = runCalculation(function($a, $b){ return $a + $b; }, 5, 10);
echo $result;  // Outputs: 15
?>

Closures and Object Scope

Closures can also be bound to an object’s scope, allowing them to access the object’s private and protected properties.

<?php
class Test {
    private $value = 1;
    public function getClosure() {
        return function() { return ++$this->value; };
    }
}
$test = new Test();
$closure = $test->getClosure();
$closure = $closure->bindTo($test, 'Test');
echo $closure();  // Outputs: 2
?>

Static Variables

Closures can maintain state between invocations using static variables.

<?php
$counter = function() {
    static $count = 0;
    $count++;
    return $count;
};
echo $counter();  // Outputs: 1
echo $counter();  // Outputs: 2
?>

Serialization

Since PHP 7.4, closures can be serialized and deserialized, making it easier to pass them around or store them for later use.

<?php
$serializeClosure = function($value) {
    return $value * 2;
};
$serialized = serialize(new SerializableClosure($serializeClosure));
$unserializedClosure = unserialize($serialized);
echo $unserializedClosure(5);  // Outputs: 10
?>

Conclusion

Closures in PHP offer a robust way to create anonymous functions that can capture variables from the surrounding scope and maintain state. By mastering closures, you open the door to a more functional programming style within PHP, with greater modularity and separation of concerns in your codebase.

Next Article: Arrow Functions in PHP: A Complete Guide

Previous Article: Optional Type Declarations in PHP: Enhancing Code Reliability

Series: Basic PHP Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array