CreateObject() In ColdFusion 9 No Longer Requires The Type Parameter

Posted July 1, 2010 at 9:30 AM by Ben Nadel

Tags: ColdFusion

I can't remember who told me this, but a while back, someone let me that the CreateObject() function in ColdFusion 9 no longer requires the "Type" argument to be passed in if you are creating a ColdFusion component. I've been wanting to test this for myself, but up until this last Friday, my ColdFusion 9 instance was broken. Now that I'm up and running again, I thought I would confirm that this is, indeed, the new ColdFusion 9 CreateObject() behavior.

To test this, I created a very simple ColdFusion component, Tricia.cfc:

Tricia.cfc

  • <cfcomponent
  • output="false"
  • hint="I am a simple component.">
  •  
  • <!--- Define a few test properties. --->
  • <cfset this.name = "Tricia" />
  •  
  • </cfcomponent>

Then, I wrote a piece of test code that instantiates the Tricia object using CreateObject() with nothing but the class path:

  • <!---
  • Create object with the CreateObject() function. Notice that
  • we no longer need to supply a TYPE argument in ColdFusion 9.
  • --->
  • <cfset tricia = createObject( "Tricia" ) />
  •  
  • <!--- Output the object name. --->
  • <cfoutput>
  •  
  • Name: #tricia.name#
  •  
  • </cfoutput

If I run this in ColdFusion 8, I get the following ColdFusion error:

Parameter validation error for the CREATEOBJECT function. The function accepts 2 to 7 parameters.

This is because, in ColdFusion 8, I need to supply, "component," as the first function argument. But, if I run this in my ColdFusion 9 instance, I get the following page output:

Name: Tricia

As you can see, in ColdFusion 9, the CreateObject() function can now create ColdFusion components by default, using just the class path. Of course, ColdFusion 9 also allows you to use the New operator as well, which is certainly less to type. But, if you ever need to create an object using a dynamically defined class path, ColdFusion 9 gives you one less thing to type.




Reader Comments

Jul 1, 2010 at 9:45 AM // reply »
46 Comments

Ben,
I actually ran into this small feature in code i was writing. I actually forgot to put a type in there and couldnt figure out why code was breaking on 8 and not 9.

-Matthew


Jul 1, 2010 at 9:48 AM // reply »
2 Comments

Even better. Try this:

<cfset tricia = new tricia() />

Regards,

Frank


Jul 1, 2010 at 9:48 AM // reply »
11,246 Comments

@Matthew,

I'm looking in the ColdFusion 9 live docs, and I can't find this feature. It also looks like CF9 didn't add any features according to the "History" of this function.


Jul 1, 2010 at 9:49 AM // reply »
11,246 Comments

@Frank,

Yeah, the NEW operator is cool. I tried to make reference to it in the blog post.


Jul 1, 2010 at 9:52 AM // reply »
319 Comments

I'd file a report on the livedocs page, _and_ file a doc bug for it. Make Adobe either document it or 'fix it' (by that I mean, if it was unintentional, the ability should be removed - I think it should stay though)


Jul 1, 2010 at 10:35 AM // reply »
11,246 Comments

@Raymond,

Can I file documentation bugs in the bug database? Oh, do you mean on the bottom of the comments in Livedocs? Cool - I've never done that before.


Jul 1, 2010 at 10:39 AM // reply »
319 Comments

You can do both - and should. :)


Jul 1, 2010 at 10:44 AM // reply »
11,246 Comments

@Raymond,

Doing it right now :)


Jul 1, 2010 at 11:04 AM // reply »
6 Comments

I'm looking forward to using the "new" syntax. you'd have to fully specify the package of the component, right?

More and more I appear to be abandoning code portability between versions for cool syntactic candy. Not sure how I feel about that.


Jul 1, 2010 at 11:06 AM // reply »
11,246 Comments

@Brian,

You can either provide the full path in the NEW target:

<cfset com = new path.to.Class() />

Or, you can import the class package and then use NEW with the pathless name:

<cfimport path="path.to.*" />
<cfset com = new Class() />

It's really exciting stuff.


Jul 1, 2010 at 3:47 PM // reply »
8 Comments

I myself prefer this

<cfset obj = new "#Request.p_cfc_root#.component"() />

to importing (assuming, of course, that the component contains the init() method).


Jul 1, 2010 at 3:51 PM // reply »
11,246 Comments

@Edy,

Oh dangy - I did not know that you could use a dynamic class path when invoking the NEW operator. Very slick!


Jul 1, 2010 at 4:15 PM // reply »
2 Comments

Even after 12 years programming in Coldfusion I still learn new cool features every day.


Jul 2, 2010 at 4:57 AM // reply »
14 Comments

@Ben

Would not giving the type in createObject be the same as not providing a scope for a scoped variable? and have the same problems attached?

or does it just assume that if no type is provided then it will be a component?


Jul 7, 2010 at 8:47 AM // reply »
25 Comments

I haven't dived into CF 9 yet :( .... we are still on CF8 but I really can't wait to start building production applications utilizing all the cool new syntax changes in CF9. Great stuff.


Jul 8, 2010 at 10:44 AM // reply »
11,246 Comments

@Frank,

Heck yeah! This language rocks!

@Tom,

I assume that internally, ColdFusion is just checking the number of arguments provided. If there is one, assume it is the class path. Does this add any overhead (like using unscoped variables)... perhaps a tiny tiny bit. That said, now that the logic is in there (assuming it is a tiny check), NOT using the feature doesn't make the code faster - it still has to perform the check.

@Hussein,

Yeah, I am still on CF8 in production as well. I can't wait to upgrade!


Nov 19, 2010 at 5:15 AM // reply »
3 Comments

Hi Ben,
I tried to run your POIUtility.cfc component on CF9, and changed the instantiation to:
<cfset objPOI = CreateObject("POIUtility")/>

But i get an error on that line:
Local variables must be initialized.

What am I doing wrong?

Thanks.
Mila.



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 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
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 »
InVision App - Prototyping Made Beautiful With Prototyping Tools