How do I get around a lack of constructors in CFCs?

A widely adopted practice is to create an "init" method that returns the object.

Example:

<cfcomponent displayname="Person">
    <cffunction name="init" access="public" output="false">
       <cfreturn this />
    </cffunction>
</cfcomponent>

The init method can accept arguments and perform object initialization, if needed. Now, you can always create your objects like this:

<cfset joe = createObject('component', 'Person').init() />

This question was written by Hal Helms
It was last updated on June 12, 2006.

Categories

Components

Comments

comments powered by Disqus