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:
<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:
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.
Want to use code from this post? Check out the license.
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.
Even better. Try this:
<cfset tricia = new tricia() />
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.
Yeah, the NEW operator is cool. I tried to make reference to it in the blog post.
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)
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.
You can do both - and should. :)
Doing it right now :)
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.
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.
I myself prefer this
<cfset obj = new "#Request.p_cfc_root#.component"() />
to importing (assuming, of course, that the component contains the init() method).
Oh dangy - I did not know that you could use a dynamic class path when invoking the NEW operator. Very slick!
Even after 12 years programming in Coldfusion I still learn new cool features every day.
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?
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.
Heck yeah! This language rocks!
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.
Yeah, I am still on CF8 in production as well. I can't wait to upgrade!
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?