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 »
11,238 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 »
11,238 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 »
16 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 »
42 Comments

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


Jul 8, 2010 at 10:39 AM // reply »
11,238 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 »
22 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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools