Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Dealing with variable encapsulation and objects and methods is always a lot of trouble. Making sure that you make all of your methods thread-safe by keeping their variables all tucked inside nice and neat can be laborious. And it's all too easy to miss a var keyword in front of a variable to keep it local to the function. ColdFusion 9 introduces a new scope. The LOCAL scope, that is confined to a specific method or function, making it much easier to make sure you don't accidentally leak variables outside of your functions. To demonstrate this in the Chapter4 folder, open localscope.cfm.
In here, I have a function written for you already that shows you all the different ways you can get variables into the LOCAL scope and when variables will not be in the LOCAL scope. So I'm just going to run through this function real quick and then we will view it in the preview and see how the dump shows where variables are getting assigned. So, in the testFunction, in these first two variable declarations you can see that the first one says cfset LOCAL.FirstName, and the second one says cfset var LastName.
The first thing you should be thinking if you have used the var keyword even once is that this is function is going to through an error because all variables with the var keyword have to be at the beginning of the function. In ColdFusion 9 that's changed. You can put a var statement on any variable that you want, anywhere in the function. And it will automatically assign that particular variable through the LOCAL scope. So, again these first two lines have either explicitly declared the LOCAL scope, or used the var keyword in order to force these variables to be inside the LOCAL scope.
Once a variable has been declared in the LOCAL scope, then if you set that value without referencing the scope, it will assume that it's in that LOCAL scope. So, in this case, these reset the values in the LOCAL scope. Even though I don't have the scope defined on the front. Now, down here where I am setting City and State, because these haven't been previously var-ed, they haven't been declared in the LOCAL scope. They are going to be put inside the global variables scope, which is available outside of the function.
So, let's take a look at this in our preview and see exactly what happens. In Safari, I can see my LOCAL scope dump inside of the function and you can see that LOCAL also includes arguments. So if I do LOCAL.arguments. testargument, I would get Dan. The same as if I just did arguments.testargument. Here we can see Terrence and Ryan as the FIRSTNAME and LASTNAME and inside our LOCAL scope. Then I have just dumped the variable scope inside the function and the variable scope outside the function.
And I have limited them to just to show these specific keys so we don't have a whole bunch of garbage to display. So, if I go back to my source code, and change the City and State to have the var in front of them, you can now see that they have been moved into the LOCAL s cope, and are not available outside the function. My other option is to just to prefix them with LOCAL. So, I will do that for both City and State, preview the file again, and you can see it has the exact same effect.
The City and the State are now inside of the LOCAL scope. If you're like me, you've made a habit of defining a local structure in order to keep your variables inside the function. So almost all of my methods and functions start like this. I've always created a LOCAL scope in order to be able to just use variables willy-nilly without having to worry about always varing every single thing. So, if I go and save this and preview again, it has no effect whatsoever on the actual functionality of the page.
ColdFusion is smart enough to know that I'm just really carrying a LOCAL scope and it will ignore that statement. So that's all there is to the LOCAL scope. You still need to make sure that you var all of your variables if you're not going to use the explicit LOCAL scope notation. If you are like me and you have already made a habit of creating a LOCAL scope in your functions and methods, this is going to really change from the way you have always worked. The biggest takeaway is that you can now var your variables anyplace you want. And in fact with the new chained operators that I talked about in an earlier video, you can even var all of your variables in one fell swoop.
So for example, I can, cfset var Firstname = Lastname = empty string. And I can var all my variables in one shot.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 102026 Viewers
61 Video lessons · 88745 Viewers
71 Video lessons · 72565 Viewers
56 Video lessons · 104207 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.