ColdFusion 9's NEW Operator Can Use Dynamic Class Paths

Posted July 6, 2010 at 9:33 AM by Ben Nadel

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

Jul 6, 2010 at 12:04 PM // reply »
2 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". ;)


Jul 6, 2010 at 12:09 PM // reply »
10,640 Comments

@Jason,

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


Jul 6, 2010 at 12:19 PM // reply »
7 Comments

Great post. Thanks for sharing this information about the NEW operator in ColdFusion 9.


Jul 6, 2010 at 4:01 PM // reply »
8 Comments

Hi Ben, what is benefits using NEW instead of createObject?


Jul 6, 2010 at 5:45 PM // reply »
10,640 Comments

@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!


Jul 6, 2010 at 7:06 PM // reply »
14 Comments

Thank you! That is great to know.


Jul 7, 2010 at 9:53 AM // reply »
4 Comments

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


Jul 7, 2010 at 12:48 PM // reply »
38 Comments

@Jason, I think u still need cfinvoke, unless you're ok with Evaluate().


Jul 8, 2010 at 10:39 AM // reply »
10,640 Comments

@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.


Mar 10, 2011 at 7:42 AM // reply »
1 Comments

Hi Ben,

Are you aware of any documentation for how the new operator resolves the location of cfc's?

Tom


Sep 14, 2011 at 2:49 AM // reply »
21 Comments

It's a shame we still cant go:

myCfc."dynamicMethodName"()

or even:

myCfc[dynamicMethodNameVar]()


Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 8, 2012 at 11:09 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Here's a warning about using fixed top or bottom menu bars that hasn't been mentioned. Browsers don't factor fixed bars into the page height for page up and page down, meaning you'll have anything f ... read »
Feb 8, 2012 at 10:32 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
@Andy, Ah, very cool. FW/1 really seems to be quite well-rounded these days! ... read »
Feb 8, 2012 at 9:52 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
Just wanted to let you know that version 2.0 of Sean Corfield's FW/1 supports routing. This allows you to build true RESTful APIs using ColdFusion. (search for "URL Routes" https://github ... read »
Feb 8, 2012 at 2:05 AM
Creating A Fixed-Length Queue In JavaScript Using Arrays
Cool site ... read »
Feb 7, 2012 at 5:00 PM
Ask Ben: Ending ColdFusion Session When User Closes Browser
We've used code that sets the cookies without the "expires" attribute in most of our applications to accommodate an "automatic logoff" (Let's face it, that's what we're really try ... read »
Feb 7, 2012 at 10:10 AM
Ask Ben: Building An AJAX, jQuery, And ColdFusion Powered Application
Hey Ben great post. Just like @Carl Steinhilber I am having the same trouble with the -'parseerror'. But I can only reach GET contacts-demo.cfm I have added secureJSON="no" ... read »
ang
Feb 7, 2012 at 4:46 AM
Using The Apple iPod Shuffle Without iTunes
i got the same error with munkey!! help please!! ... read »
Feb 7, 2012 at 2:48 AM
Ask Ben: Javascript String Replace Method
@Kadut, . is a special character you will need to escape it \. ... read »