Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at Scotch On The Rock (SOTR) 2010 (Brussels) with:

ColdFusion 9's NEW Operator Can Use Dynamic Class Paths

By Ben Nadel on
Tags: ColdFusion

Last week, I discussed the fact that in ColdFusion 9, the CreateObject() function no longer needs the "Type" parameter when creating ColdFusion components. ColdFusion 9 also introduced the New operator for component creation; but, I had stated that if you needed to create components with dynamic class paths, the CreateObject() method was great for this purpose. In the comments to that blog post, however, Edy Ionescu pointed out that the "New" operator in ColdFusion 9 can also use dynamic class paths. This kind of blew my mind and I needed to try it immediately.

To test this functionality, I created a simple ColdFusion component, Tricia.cfc, with a single public property, "Name." Then, I created a test script that instantiated said component using a class path variable:

  • <!--- Define the dynamic class path of the target component. --->
  • <cfset classPath = "Tricia" />
  •  
  • <!--- Use the NEW operator with the dynamic class path. --->
  • <cfset girl = new "#classPath#"() />
  •  
  • <!--- Output the girl's name. --->
  • <cfoutput>
  •  
  • Name: #girl.name#
  •  
  • </cfoutput>

As you can see here, I am using a quoted variable name to define the class path and then calling the "()" operator right after it. When we run this code, we get the following output:

Name: Tricia

This worked perfectly. Of course, using quoted values to create dynamic variable names in ColdFusion has been around for a long time. But, it's awesome to see this working in conjunction with the "New" and "()" operators! It looks like creating basic ColdFusion components in ColdFusion 9 has no more need of CreateObject(). I can't see that it offers anything over the New operator at this point.




Reader Comments

Totally agree, there is really no reason to use cfobject/createobject/cfinvoke when instantiating CFC's. There still useful for COM/Java/Webservices/etc. But for plain old CFC's, just use "new". ;)

Reply to this Comment

@Jason,

Yeah, I'm really like the New operator a lot. Now, I just need to upgrade more of my servers!

Reply to this Comment

@Misha,

For one, there's simply less to type; and the NEW operator is more in alignment with how other languages perform class instantiation. But, there is also some implicit wiring that happens. The NEW operator gives you control over which method is invoked as a constructor and what value that constructor returns.

@Don,

Thanks my man!

Reply to this Comment

I wonder if there are any performance implications for creating objects this way.

Reply to this Comment

@Tim,

Yeah, this is quality stuff! I really need to upgrade sooner than later.

@Brian,

ColdFusion is designed to allow for dynamic variable names in general. I think this is just part of that functionality; I don't think particular instance affects performance.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.