Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There's no argument that CFCs make our development lives easier by encapsulating our code and allows us to develop rich domain models. Unfortunately, creating CFCs and dealing with pathing issues is always a pain, with long statements needed to simply create a new instance of an object. ColdFusion 9 solves this by letting us imports CFC paths into a page and use the New keyword to generate new instances of objects, instead of using the old CreateObject syntax. So in the Chapter3 folder, open up the newandimport.cfm file.
And here you can see the "old style" of instantiating an instance of an object. In this case, I am doing, cfset MyCFC = CreateObject("component", "Chapter3.cfcs.MyCFC"). And if you look here in my Chapter3 folder, I have a MyCFC.cfc. All it is does is set two properties and has an init statement that sets the default values for those properties. Now that we know all there is to know about that component, we'll go ahead and close that and I'll go ahead and run this page just to see what we get for our output.
And in this case we simply get the component with the one method defined on it. However, usually when I instantiate a CFC, I run the init method on it so I can chain the init method. We'll save that and run it, and now we can see that it's set the default properties for LASTNAME, ID, and FIRSTNAME. So the only thing I don't like about working with CFCs is this business. I end up typing CreateObject component over and over and over and over again.
Now in ColdFusion 9, you can now use the New keyword to create a new instance of an object. So I can replace this statement with this. I'll do cfset MyCFC = New Chapter3.cfcs.MyCFC, and that's it. So quite a few fewer characters. So I am going to go ahead and delete this line, save my file, and preview it. And you can see we get the exact same thing that we got with the previous code, but I didn't call the init method.
When you use the New keyword, the init method is called by default when the object is instantiated. If we don't have an init method it simply doesn't get called. So let's go back over here to the MyCFC file and just remove the init function. Save it, go back over here and reload. And you can see I just don't get my ID, FIRSTNAME, and LASTNAME, and if I add that back, refresh my page, then the init method gets called again.
That's already wonderful right there. I have I've already got rid of at least 20 characters of typing that I have to do every time that I want to instantiate a new component. But it could still be easier. We can now use the cfimport tag to specify a path that contains our CFCs. So in this case, I am going to set my path to Chapter3.cfcs.*, which means I want to import all components that are in the Chapter3.cfcs directory. Then I can simply erase this part, save my file, test.
And boom, it's exactly the same as it was before. I can even add this statement into my Application.cfc and it would filter down through my entire application. While this is easy enough, it doesn't always filter down through the entire request. So make sure that you test your applications as you are going forward to make sure that they pick up that import statement. And using custom mappings are still easier than using the cfimport syntax. But there is definitely no argument that this is a far easier way to get new objects instantiated.
So use the New keyword to quickly create new objects and get your apps built that much quicker.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 102069 Viewers
61 Video lessons · 88784 Viewers
71 Video lessons · 72595 Viewers
56 Video lessons · 104237 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.