PHP BOOTCAMP: Closures Demystified
Yo DAWG, Here is a function for your function.
If you have worked with Javascript a lot, you would have used functions inside functions several times. I mean anonymous functions ( functions without a name, I call them bastard functions ). I also mean inner functions that possess the ability to access properties within the scope of the outer functions.
In PHP, Closures exist and it’s as powerful as the name sounds. Wait,..Holup!, Holup!..Are you currently thinking this is Clojure. Nah, Please do me a favour, it is Closure!. Clojure is just another programming language and I have a colleague of mine that blabs about it all day, Damn!!!
Come, let’s take the first step, We can assign a nameless function to a variable like so:
1 2 3 4 5 6 7 |
<?php $bootcamp = function(){ echo "I want to learn Modern PHP"; }; ?> |
Then, we can call the function like so:
1 2 3 4 5 |
<?php echo $bootcamp(); ?> |
So, our result would be:
1 |
I want to learn Modern PHP |
Awesome!
This is an anonymous function, but PHP developers loosely call them lambdas and Closures.
Now, there is actually a distinction. An ordinary standalone anonymous function is a Lambda.
A Closure is an anonymous function that can exist inside another function and have the ability to access the properties and parameters of the outer function.
There is also an inbuilt Class called Closure.
Now, Let’s see why we really need Closures if you may ask!
The ability to pass functions into other functions is what makes Closures dope!. There are several situations where you’ll want to pass several tiny bits of sweet logic into a function and as long as you are using PHP 5, I mean PHP >= 5.3 version,..then you are safe to use it in your application.
Let’s see a cool example of that.
1 2 3 4 5 6 7 8 9 |
<?php function getResponse(Closure $form, array $values){ return $form( $values ); } ?> |
For the getResponse function, I passed in two type-hinted parameters…an array and a closure.
Type-hint is basically specifying the type of a parameter passed into a function. It’s to protect anyone from just passing any type of value into a function.
Now, let’s write a closure.
1 2 3 4 5 6 7 8 9 |
<?php $toJson = function(array $values){ return json_encode( $values ); }; ?> |
Another One:
1 2 3 4 5 6 7 8 9 |
<?php $flipResult = function(array $values){ return array_flip( $values ); }; ?> |
Now we have $toJson Closure and $flipResult Closure.
Let’s use them with our getResponse function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php function getResponse(Closure $form, array $values){ return $form($values); } $toJson = function(array $values){ return json_encode($values); } $flipResult = function(array $values){ print_r(array_flip($values)); } echo getResponse($toJson, ['prosper', 'godson', 'chidi']); echo getResponse($flipResult, ['prosper', 'godson', 'chidi']); ?> |
Our results would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//First result { "prosper", "godson", "chidi" } //Second Result Array ( [prosper] => 0 [godson] => 1 [chidi] => 2 ) |
That’s how easy it is to use Closures
Please, if you have any questions or observations, Let me know in the comments section.
- Windows Users: Use Launchy - August 31, 2015
- Windows Users: Zsh To Glory Using Babun - August 31, 2015
- Brace Up, PHP 7 is Changing The Game!!! - August 27, 2015
- PHP BOOTCAMP: Closures Demystified - August 25, 2015
- Using Twitter Authentication For Login in Laravel 5 - August 24, 2015
- Using Facebook Authentication For Login in Laravel 5 - August 24, 2015
- How to Grayscale Images in Laravel 5 - August 20, 2015
- Think Like a Git, Commit Like a Boss: Squash like a Pro - August 20, 2015
- Think Like a Git, Commit Like a Boss: Save Time By Aliasing - August 19, 2015
- Think Like a Git, Commit Like a Boss: Revert Now! - August 18, 2015