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 the New York ColdFusion User Group (Jan. 2008) with:

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

By Ben Nadel on
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

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

Reply to this Comment

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

Reply to this Comment

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)

Reply to this Comment

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

Reply to this Comment

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.

Reply to this Comment

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

Reply to this Comment

I myself prefer this

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

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

Reply to this Comment

@Edy,

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

Reply to this Comment

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

Reply to this Comment

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

Reply to this Comment

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.

Reply to this Comment

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

Reply to this Comment

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.

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.