Video: Using New and Import

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.

In ColdFusion 9 New Features, author and developer Dan Short has gathered together the important updates and improvements in this dynamic web application. Dan showcases the new ColdFusion Builder application throughout the course, using it to work through the new language and CFScript enhancements in this release. He shows how to use the new Server Manager to compare servers and create server groups, as well as monitor server health through notifications. He also explains the new integration with Office applications, building Excel, Word, and even PowerPoint files on the fly from ColdFusion. Finally, he covers the important new Object Relational Mapping feature and how to use the built-in Flash Player. Exercise files accompany this course.

Using New and Import

There's no argument that CFCs make our development lives easier by encapsulatingour 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 theNew keyword to generate new instances of objects, instead of using the oldCreateObject 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 doesis set two properties and has an init statement that sets the default valuesfor those properties.Now that we know all there is to know about that component, we'll go aheadand 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 canchain the init method.We'll save that and run it, and now we can see that it's set the defaultproperties 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 theobject 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 least20 characters of typing that I have to do every time that I want toinstantiate 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 Iwant 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 downthrough 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 makesure 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 newobjects instantiated.

So use the New keyword to quickly create new objects and get your appsbuilt that much quicker.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.