Initializing instance data

Some components have instance data, which is data that persists as long as the component instance exists. For example, a shopping cart component can have instance data that includes the IDs and quantities of items that the user places in the shopping cart. Instance data is often shared by several methods that can create, delete, or modify the data.

You can refer to instance data of a CFC only if you create an instance of the CFC. From inside the CFC, you refer to instance data of the CFC using the this prefix, for example this.firstvariable. From the calling page, you refer to instance data using dot notation, including the name of the instance of the component and the name of the instance data, as in objectname.ivarname. Components whose methods you invoke without first instantiating the component do not typically have instance data.

You initialize instance data at the top of the component definition, before the method definitions. ColdFusion executes this code when it instantiates the component; for example, when a cfobject tag creates the component instance. Because this code executes only when the instance is created and it typically “constructs” properties of the component, instance data initialization code is sometimes called constructor code.

You can use any CFML tag or function in constructor code, and the code can perform any ColdFusion processing, such as querying a database or data validation and manipulation. If one component extends another, the parent component’s constructor code executes before the child component’s constructor code.

Note: ColdFusion does not require you to place the initialization code at the top of the component definition; however, it is good programming practice to do so.

The following example shows constructor code for a shopping cart CFC:

<cfcomponent> 
    <!--- Initialize the array for the cart item IDs and quantities. ---> 
    <cfset This.CartData = ArrayNew(2)> 
    <!--- The following variable has the ID of the "Special Deal" product for  
            this session. ---> 
    <cfset This.Special_ID = RandRange(1, 999)>

For information on scopes, see The This scope and The Variables scope.

A useful technique is to define a method named init(), which initializes an instance of a CFC, acting as a constructor. The init() method can initialize constants and return an instance of the component to the calling page. The following code illustrates an example of an init() method:

<cfcomponent displayname="shoppingCart"> 
    <cffunction name="init" access="public" output="no" returntype="shoppingCart"> 
        <cfargument name="shoppingCartID" type="UUID" required="yes"> 
        <cfset variables.shoppingCartID = arguments.shoppingCartID> 
        <cfreturn this> 
    </cffunction> 
 
    <!--- Additional methods go here. ---> 
</cfcomponent>

In this example, the init() method uses the variables scope to make the shopping cart ID available anywhere in the CFC. For more information about scope, see CFC variables and scope.