From the course: Functional Programming with PHP

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Closure

Closure - PHP Tutorial

From the course: Functional Programming with PHP

Start my 1-month free trial

Closure

- [Instructor] In a previous video, we talked about how in PHP it's possible to return functions from other functions, and saw some possible applications of doing this in our code base. Returning functions from other functions leads us to another very important concept in PHP, and this is something called closure. So, what is closure exactly? Basically, closure means that when we define a function that returns another function, the function that we return sometimes still requires access to the scope that it was defined in. In other words, the internal scope of the function that returned it. Now, in most other commonly used languages that support first class functions, such as JavaScript and Python, this happens automatically. But in PHP, there's a little bit of extra notation we have to include to make this happen. To see what I mean, let's code out an example that demonstrates closure. The first thing we're going to do…

Contents