1 00:00:00,560 --> 00:00:03,270 Next up is something you may not use often, but 2 00:00:03,270 --> 00:00:06,270 is a good thing to know, and those are closures. 3 00:00:06,270 --> 00:00:10,060 Closures are anonymous functions, which are functions with no name, 4 00:00:10,060 --> 00:00:14,190 that are capable of accessing variables outside of the function scope. 5 00:00:14,190 --> 00:00:17,540 We'll talk more about the use of closures as call backs when we 6 00:00:17,540 --> 00:00:19,550 get to the internal functions video. 7 00:00:19,550 --> 00:00:21,530 For now, let's look at how this works. 8 00:00:24,330 --> 00:00:27,860 Okay, to provide an example of a closure, let's start by opening and 9 00:00:27,860 --> 00:00:30,690 closing a set of PHP tags. 10 00:00:30,690 --> 00:00:33,720 Then inside of those, we're gonna do it a little bit differently. 11 00:00:33,720 --> 00:00:36,760 Before we had used the function keyword, right? 12 00:00:36,760 --> 00:00:39,330 And then added a name for our function. 13 00:00:39,330 --> 00:00:44,000 In this case, we're going to use $greet as a variable, and 14 00:00:44,000 --> 00:00:48,620 then set it equal to function, and then open and close parentheses. 15 00:00:48,620 --> 00:00:51,310 And then open and close our curly braces, and 16 00:00:51,310 --> 00:00:55,850 then follow that as a statement, enclosing it with a semi-colon. 17 00:00:55,850 --> 00:01:01,270 So what this is going to do is whatever we do inside this function, it's going to set 18 00:01:01,270 --> 00:01:07,780 its output to greet, $greet will contain whatever function produces. 19 00:01:07,780 --> 00:01:08,760 So let's do this. 20 00:01:08,760 --> 00:01:13,990 We'll add inside the function echo Hello there and then close that statement. 21 00:01:13,990 --> 00:01:18,800 This way, when we call $greet then we'll get hello there. 22 00:01:18,800 --> 00:01:22,910 Now let's say we have something outside of the function scope. 23 00:01:22,910 --> 00:01:26,740 Well anonymous functions are not able to access things outside of 24 00:01:26,740 --> 00:01:31,400 the function scope unless we utilize the use keyword. 25 00:01:31,400 --> 00:01:36,490 So what we'll do here is if we have a variable named $name and that's equal 26 00:01:36,490 --> 00:01:41,380 to friend and we wanna use it in our greet function, or our greet anonymous function, 27 00:01:41,380 --> 00:01:47,530 then we simply say function use, and then in parentheses, $name. 28 00:01:47,530 --> 00:01:52,634 This way, inside of our statement, inside of the function we can say, 29 00:01:52,634 --> 00:01:54,686 echo Hello there, $name. 30 00:01:54,686 --> 00:01:57,049 Notice how I've surrounded it in double quotes now so 31 00:01:57,049 --> 00:02:00,300 that we can actually output the value of the string. 32 00:02:00,300 --> 00:02:02,990 So to build out our example in WorkSpaces, 33 00:02:02,990 --> 00:02:06,396 we are going to define our anonymous function or 34 00:02:06,396 --> 00:02:11,720 our closure with $greet as the name and then we'll set that equal to a function. 35 00:02:13,110 --> 00:02:15,220 And then open and close our parenthesis. 36 00:02:15,220 --> 00:02:17,000 Open and close our curly braces, but 37 00:02:17,000 --> 00:02:20,660 the big case here is to make sure you end this in a semicolon. 38 00:02:20,660 --> 00:02:23,580 So a anonymous function is really more like 39 00:02:23,580 --> 00:02:27,420 a single line function that's being assigned to a variable. 40 00:02:27,420 --> 00:02:29,490 So we'll make sure we end it in a semicolon and 41 00:02:29,490 --> 00:02:32,160 then inside we'll actually do an echo statement. 42 00:02:32,160 --> 00:02:39,390 So we'll say echo and then our echo will be hello there, okay? 43 00:02:39,390 --> 00:02:40,890 Close it with a semicolon and 44 00:02:40,890 --> 00:02:45,850 then in order to call this, we'll simply type in $greet and 45 00:02:45,850 --> 00:02:49,990 then open and closed parenthesis, just like we did in a variable function. 46 00:02:49,990 --> 00:02:52,770 Let's head over to our preview and take a look. 47 00:02:52,770 --> 00:02:57,120 Okay, so you'll see here we have hello and in there exclamation point. 48 00:02:57,120 --> 00:02:59,140 So, that's exactly what we're looking for. 49 00:02:59,140 --> 00:03:02,370 Okay, so let's head back over to WorkSpaces and 50 00:03:02,370 --> 00:03:05,030 build out this example just a little bit more. 51 00:03:05,030 --> 00:03:06,992 Let's say we wanna greet a name. 52 00:03:06,992 --> 00:03:09,200 All right, so we'll say function and 53 00:03:09,200 --> 00:03:13,530 then we want something outside of the function scope. 54 00:03:13,530 --> 00:03:18,610 So we'll have something like name, and then we'll set that equal to Mike. 55 00:03:18,610 --> 00:03:19,390 All right. 56 00:03:19,390 --> 00:03:22,740 So inside of our function we want to access $name. 57 00:03:22,740 --> 00:03:27,760 Well we can't just pass it through as an argument and we don't want to use global. 58 00:03:27,760 --> 00:03:30,290 So we can use the keyword, use. 59 00:03:30,290 --> 00:03:35,040 So we'll say use, and then what we want to use is $name. 60 00:03:35,040 --> 00:03:41,890 So now we can go through and say hello and then $name. 61 00:03:41,890 --> 00:03:44,660 But we want to make sure we change this to double quotes because we 62 00:03:44,660 --> 00:03:49,080 are evaluating name and single quotes don't do evaluation. 63 00:03:49,080 --> 00:03:52,010 So let's go ahead and hit Save and see if this does what we expect. 64 00:03:53,670 --> 00:03:54,760 Refresh. 65 00:03:54,760 --> 00:03:56,540 And then it says, hello Mike. 66 00:03:56,540 --> 00:04:00,870 So in an anonymous function, instead of passing through an argument anonymously, 67 00:04:00,870 --> 00:04:04,680 we'll just use something outside of the function scope. 68 00:04:04,680 --> 00:04:08,280 So name would be outside the function scope defined on line three. 69 00:04:08,280 --> 00:04:12,750 We utilize the use keyword and then pass through $name that way