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.