Shall we get a little closure to closures? Snuggle up a bit? Okay, that was a bad one. It's a weird word though, isn't it? Closures are special functions. These types of functions don't have a name. Much like a Samurai!
Who needs a name anyway?
<?php
function () {
return 'bar';
}
Great! Wait, not great. How do we call a function without a name? Well Closures are special, we can assign them to variables just like we would with a simple value. Let's take a look at the syntax for this.
<?php
$cat = function () {
echo 'Oh long Johnson!';
};
Here, we've assigned an anonymous function, or 'Closure', to a variable called $cat
. The function simply returns a string associated with a popular feline meme.
Note that we have a semi ;
colon appended to our function. This is because it's part of an assignment statement, and functions like any other single line of PHP.
Now that we've trapped this Closure within our variable, we can call it with rounded brackets, just like any other function! For example:
<?php
$cat();
We simply append the brackets to our variable, and the code is executed.
Oh long Johnson!
Passing functions to functions...
Closures arrived in PHP 5.3, which means that they're fairly recent! They were a game-changer for the PHP world, because they allow you to pass small sections of logic into functions. That's right, you can pass a Closure to a function. Let's see this in action.
Oh, by the way, did I mention that you can type-hint a Closure? Simply place the word Closure
before a parameter to ensure that only a closure is passed as a parameter to the method. If you were to call get_type()
on a Closure, you'd receive the value Closure
. It's a complex object in PHP. We'll see other objects of this type later.
Where were we? Oh yes, an example!
<?php
// Create a math function.
function math(Closure $type, $first, $second) {
// Execute the closure with parameters
return $type($first, $second);
}
Okay, what's going on here?
First of all, we've got a function called math()
, but it's not doing a lot of math()
at the moment. It receives a type-hinted Closure, and two other variables as parameters.
Well, we're not going to be able to call it without a Closure, are we? Let's make a few Closures so that we can test it. We'll create Closures that perform mathematical operations. I know, I know. There's a lot of math, isn't there? I promise to keep it basic!
<?php
// Create a math function.
function math(Closure $type, $first, $second) {
// Execute the closure with parameters
return $type($first, $second);
}
// Create an addition closure.
$addition = function ($first, $second) {
// Add the values.
return $first + $second;
};
// Create an subtraction closure.
$subtraction = function ($first, $second) {
// Subtract the values.
return $first - $second;
};
We've defined two Closures bound to the variables $addition
and $subtraction
. They both receive two parameters, and perform subtraction or addition on these values. Let's try to use them with our math()
function, shall we?
<?php
// Create a math function.
function math(Closure $type, $first, $second) {
// Execute the closure with parameters
return $type($first, $second);
}
// Create an addition closure.
$addition = function ($first, $second) {
// Add the values.
return $first + $second;
};
// Create an subtraction closure.
$subtraction = function ($first, $second) {
// Subtract the values.
return $first - $second;
};
// Execute math function.
echo math($addition, 2, 2);
echo PHP_EOL; // New line!
echo math($subtraction, 5, 3);
The PHP_EOL is a constant that will be replaced with a newline character that's compatible with the current operating system. It's super handy!
We call the math()
function twice with a different assigned Closure each time, providing integers, and separating the calls with a newline character using the PHP_EOL
constant. Let's take a look at the output.
4
2
Great! By passing a different Closure to out math()
function, we've changed the way that it works. For homework, why don't you try changing the above code to include multiplication and division functionality? Here's a hint, you're going to want more Closures!
My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.
It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!