I’m not sure how I missed this before but among the long list of things added to ColdFusion 9 is the ability to create a CFC using the “New” syntax. Up until now, to create a new object from a CFC we’d use the createObject() method like so

<cfset Team = createObject("component", "model.Team") />

Now, using the New keyword, we can shorten that to

<cfset Team = New model.Team() />

But wait, there’s more!
When you use the New keyword, ColdFusion will automatically look for and run any init() method that exists in the CFC. It also respects any arguments that your init() method specifies, meaning that if you have a Team.init() method that can accept team name, color and manager arguments, you can build them right into your the same line of code used to create your object like so

<cfset Team = New model.Team( "Bumblebees", "Yellow", "Benny Bee" ) />

Likewise, as you’d expect, named arguments are still supported. So if you had to send in arguments out of order or needed to omit some optional arguments at object creation, you could do

<cfset Team = New model.Team(
              manager="Benny Bee",
              color="Yellow",
              name="Bumblebees") />

I personally am going to make a point to start using this in my code because I can never seem to spell the word “component” correctly in the createObject() method.

16 thoughts on “Embracing “New” in ColdFusion 9

  1. “new” must be embraced by the CF community! I was very happy to see your post Dan, keep spreading the good word :-)

    Just as a point to note, you can use “import” statements at the top of the file to effectively import a package, which means you could then omit the fully-qualified path when instantiating a new object from a CFC via “new” that exists in that package; import model.*; team = new Team(); If you’re going to create a lot of objects from a specific name-space, it will save keystrokes.

  2. @Brian Thanks for that tidbit on the import statement. I can see where that might come in really handy in certain situations. I’ll add that to my list of things to play with and see about integrating into how I write apps.

  3. Manithan

    new is cool. one thing just want to mention that you don’t have to say component anymore in CF9 you can use in like this createObject(?model.Team?) still it will work

  4. James Moberg

    If you are a contributor of Riaforge, using the “new” method will likely force your project to be CF9-only. While it is convenient, they syntax makes it difficult for projects to work on both CF8 & CF9 Servers. I’m not sure if this syntax would work if you wanted the code to run on other CF engines like Railo & Blue Dragon.

  5. @James – You are absolutely correct. I’m not sure what the other CFML engines do with the new() operator. In my case I’m doing most of my development these days on Adobe ColdFusion 9 so that was the frame of mind I was in when I originally wrote this post.

  6. Dawesi

    @James Moberg

    I think it’s about time CF apps moved on from legacy versions of ColdFusion, just as we did with CF5 (even though it was killer for it’s time, especially under load)…

    Bring on CF9+ and Railo 4+ and the new generation of integrated mashable applications on riaforge…

  7. Ed A

    So, here is a question, can you overload the init() function? Using your example, two possible init’s could be:

    init()
    init(,,)

    and depending on what you pass in will CF know which init to call?

  8. @Ed – ColdFusion doesn’t support method overloading like Java does. If you have more than one <cffunction> block in a component with the same name, ColdFusion will throw an exception that says “Routines cannot be declared more than once.”

  9. I have a Flex application where it remotely calls CFC methods. The values are passed using the VO between Flex and ColdFusion. One interesting I found was that when I put in get and set methods to the VOs I don’t get the values back from Flex to CFC. However, when I remove the get and set then the values are fine. Has anyone experienced this before? Any help would be much appreciated. Thanks.

  10. Rolando

    @Ed A
    I know this is an old post, but just to clarify. While CF does not support method overloading, it does support optional parameters. So you can still have a single method that can be called with different parameters as you suggested: init() or init(a,b,c).

  11. @Ed A, et al: Yes, no method overloading, but argumentCollection overload all you want.
    And you can artificially method overload.. sort of. Let’s say you arg overload init() or some other Master Business function. You can of course check the arguments passed in and if a certain signature is there, your init() or MBFN() can call other helper methods and just pass along all the arguments in the argumentCollection…. Not quite the same, but pretty close in terms of functionality…

  12. Bilal

    You can call new in this way too which is pretty interesting to allow dynamic determination of components at runtime:

    Team = new “#prefix#.model.#module#.Team”()

  13. Just stumbled upon this and couldn’t believe I missed it even into CF 10. FWIW – I am not sure whether it was CF9 or CF10, but you also do not have to use the word “component” as CF assumes it is a component unless you tell it otherwise.

Leave a Reply

Your email address will not be published. Required fields are marked *

*