new is not a ColdFusion reserved keyword

Word Count: 267
I was amazed to find out that keyword new was not a reserved word. I was fairly certain that it was not a keyword that is being used but I thought It may be reserved for latter use. With that being said and my new found love for cfcUnit (more on this later kids!) I now have a cool little utility function. Again, I got the idea from cfcUnit as they use the newObject() form. So I alway have a base class for each project that all other components or other base classes will extend. In this base class I like to have little helper functions and this new one is a great one. Here is the code to my base component.
<cfcomponent name="BaseComponent">
   
   <cfset NULL = "">
   <cfset variables["_objectID"] = CreateUUID()>
   
   <cffunction name="new" access="public" returntype="WEB-INF.cftags.component">
      <cfargument name="type" type="string" required="true"/>
      <cfargument name="object" type="string" default="component">
      <cfset var ret = "">
      
      <cftry>
         <cfset ret = createObject(arguments.object, arguments.type)>
         
         <cfcatch type="coldfusion.runtime.CfJspPage$NoSuchTemplateException">
            <cfthrow type="Exception.NoSuchComponent" message="Could not find the component '#arguments.type#'"/>
         </cfcatch>
         <cfcatch type="any">
            <cfthrow type="Exception.Instantiation" message="Could not instantiate object of type #arguments.type#" detail="#arguments.type#"/>
         </cfcatch>
      </cftry>
      <cfreturn ret>
   </cffunction>
   
</cfcomponent>

This is a really good time saver because now anytime I need to instantiate any objects in any cfc that extends my base its really easy to do.

<cfcomponent name="BaseTest" extends="BaseComponent">

   <cfset variables.shoppingCart = "">
   <cfset variables.dsn = "">
   
   <cffunction name="init" access="public" returntype="BaseTest">
      <cfargument name="dsn" type="string" required="true">
      <cfset variables.dsn = arguments.dsn>
      <cfreturn this>
   </cffunction>
   
   <cffunction name="getCart" access="public" returntype="any">
      <cfset variables.shoppingCart = new("ShoppingCart").init(variables.dsn)>      
      <cfreturn variables.shoppingCart>
   </cffunction>
   
</cfcomponent>

Comments

#1 Posted By: Phil Cooper Posted On: 2/1/07 12:13 PM
Sorry, I haven't tried this to see what happens but if you initiated an object other than a component (say a com object or java) then wouldn't it error since you are always returning and object of type 'WEB-INF.cftags.component'?
#2 Posted By: Dan Posted On: 2/1/07 12:29 PM |
Author Comment
Phil, I have not tested it either but I suspect your right. The reason I use this is for shortcut purposes to creating components. I am sure you could set the return type to any.


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.