new is not a ColdFusion reserved keyword
Thursday December 14, 2006 4:57 PM
Word Count: 267
<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>
<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>
<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>
