Contents |
The Init() Function
When using objects is very useful to be able to "initalise" them into a known state before we start using them. This is called object initalisation which is an important topic that deserves special attention, but we will introduce it here.
The convention for initialising ColdFusion components is to use a function named init():
<cffunction name="init" output="false"> <cfreturn this> </cffunction>
This example is the most basic form of the init() function.
The <cfreturn this> is very important and if not included will cause errors in your code.
The init() function frequently takes arguments needed to initialise your object.
In the previous section we introduced a ThresholdCounter object which contained a hardcoded value of 100 for the threshold level.
<cfcomponent output="false"> <cfset variables.threshold = 100> <!--- HARDCODED THRESHOLD ---> <cfset variables.currentTotal = 0> <cffunction name="addAmount" output="false" returntype="void"> <cfargument name="amount" type="numeric" required="true"> <cfset variables.currentTotal = variables.currentTotal + arguments.amount> </cffunction> <cffunction name="thresholdHasBeenReached" output="false" returntype="boolean"> <cfset var thresholdReached = variables.currentTotal ge variables.threshold> <cfreturn thresholdReached> </cffunction> </cfcomponent>
This is fine but it would be much nicer if we could set this threshold to a particular value when we create the object, then we could have multiple ThresholdCounter objects each with different thresholds if we needed.
Let's add an init() function that can accept the threshold as an argument.
<cfcomponent output="false"> <cfset variables.threshold = 0> <cfset variables.currentTotal = 0> <cffunction name="init" output="false"> <cfargument name="threshold" required="true"> <cfset variables.threshold = arguments.threshold> <cfreturn this> </cffunction> <cffunction name="addAmount" output="false" returntype="void"> <cfargument name="amount" type="numeric" required="true"> <cfset variables.currentTotal = variables.currentTotal + arguments.amount> </cffunction> <cffunction name="thresholdHasBeenReached" output="false" returntype="boolean"> <cfset var thresholdReached = variables.currentTotal ge variables.threshold> <cfreturn thresholdReached> </cffunction> </cfcomponent>
Now when we create the object we need to also call the init() function to properly initialise our object.
The preferred technique for creating and initalising objects is to create the object and initialise in one step:
<cfset counter = createObject("component","ThresholdCounter").init(100)>
Init() is not a special function name
It is important to note that init() is not a special reserved function name for ColdFusion components. It is just a convention than has been widely adopted by the ColdFusion community. There are some other reasons to use init() which relate to using Java objects within CFML.
Always use an init() function
The init() function should be included in every component you create, even if it does nothing, and it must always be called when creating your objects.
When you first create a new component the init() function may have no apparent need, but as your application grows you may find that your component changes and you need to initialise your objects into a particular state.
From Here
References and Further Reading
How to explain <cfreturn this>
SideBar
User Login