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,243 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,243 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,243 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 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
May 23, 2013 at 3:55 AM
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
very interesting and helpful too. ... read »
May 22, 2013 at 5:35 PM
Script Tags, jQuery, And Html(), Text() And Contents()
This is still an issue 2 years later. jQuery is supposed to remediate these cross browser issues, no? I have been unable to find any statement from the jQuery team calling this behavior "by de ... read »
May 22, 2013 at 12:44 PM
Ask Ben: Query Loop Inside CFScript Tags
In cf10, if you call a function that has: local.result = {}; local.result.msg = ""; local.svc = new query(); local.svc.setSQL("SELECT * FROM..."); local.obj = local.svc.exe ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools