PHP Closures4:14 with Hampton Paulk
Closures are anonymous functions, which are functions with no name, that are capable of accessing variables outside of the function scope. We will talk more about the use of closures as callbacks when we get to the internal functions video.
Next up is something you may not use often, but 0:00 is a good thing to know, and those are closures. 0:03 Closures are anonymous functions, which are functions with no name, 0:06 that are capable of accessing variables outside of the function scope. 0:10 We'll talk more about the use of closures as call backs when we 0:14 get to the internal functions video. 0:17 For now, let's look at how this works. 0:19 Okay, to provide an example of a closure, let's start by opening and 0:24 closing a set of PHP tags. 0:27 Then inside of those, we're gonna do it a little bit differently. 0:30 Before we had used the function keyword, right? 0:33 And then added a name for our function. 0:36 In this case, we're going to use $greet as a variable, and 0:39 then set it equal to function, and then open and close parentheses. 0:44 And then open and close our curly braces, and 0:48 then follow that as a statement, enclosing it with a semi-colon. 0:51 So what this is going to do is whatever we do inside this function, it's going to set 0:55 its output to greet, $greet will contain whatever function produces. 1:01 So let's do this. 1:07 We'll add inside the function echo Hello there and then close that statement. 1:08 This way, when we call $greet then we'll get hello there. 1:13 Now let's say we have something outside of the function scope. 1:18 Well anonymous functions are not able to access things outside of 1:22 the function scope unless we utilize the use keyword. 1:26 So what we'll do here is if we have a variable named $name and that's equal 1:31 to friend and we wanna use it in our greet function, or our greet anonymous function, 1:36 then we simply say function use, and then in parentheses, $name. 1:41 This way, inside of our statement, inside of the function we can say, 1:47 echo Hello there, $name. 1:52 Notice how I've surrounded it in double quotes now so 1:54 that we can actually output the value of the string. 1:57 So to build out our example in WorkSpaces, 2:00 we are going to define our anonymous function or 2:02 our closure with $greet as the name and then we'll set that equal to a function. 2:06 And then open and close our parenthesis. 2:13 Open and close our curly braces, but 2:15 the big case here is to make sure you end this in a semicolon. 2:17 So a anonymous function is really more like 2:20 a single line function that's being assigned to a variable. 2:23 So we'll make sure we end it in a semicolon and 2:27 then inside we'll actually do an echo statement. 2:29 So we'll say echo and then our echo will be hello there, okay? 2:32 Close it with a semicolon and 2:39 then in order to call this, we'll simply type in $greet and 2:40 then open and closed parenthesis, just like we did in a variable function. 2:45 Let's head over to our preview and take a look. 2:49 Okay, so you'll see here we have hello and in there exclamation point. 2:52 So, that's exactly what we're looking for. 2:57 Okay, so let's head back over to WorkSpaces and 2:59 build out this example just a little bit more. 3:02 Let's say we wanna greet a name. 3:05 All right, so we'll say function and 3:06 then we want something outside of the function scope. 3:09 So we'll have something like name, and then we'll set that equal to Mike. 3:13 All right. 3:18 So inside of our function we want to access greet. 3:19 Well we can't just pass it through as an argument and we don't want to use global. 3:22 So we can use the keyword, use. 3:27 So we'll say use, and then what we want to use is $name. 3:30 So now we can go through and say hello and then $name. 3:35 But we want to make sure we change this to double quotes because we 3:41 are evaluating name and single quotes don't do evaluation. 3:44 So let's go ahead and hit Save and see if this does what we expect. 3:49 Refresh. 3:53 And then it says, hello Mike. 3:54 So in an anonymous function, instead of passing through an argument anonymously, 3:56 we'll just use something outside of the function scope. 4:00 So name would be outside the function scope defined on line three. 4:04 We utilize the use keyword and then pass through $name that way 4:08
You need to sign up for Treehouse in order to download course files.Sign up