Using variables.instance Inside Your Components

Often when creating ColdFusion components developers will create a structure to hold component instance data. This data is stored in the components ‘variables’ scope and is therefore available to all methods.

Here is an example from the rooibos component generator:

1
2
3
4
5
6
7
8
9
<cfcomponent
  displayname="sample"
  output="false"
  hint="A bean which models the form.">

  <!---
  PROPERTIES
  --->
  <cfset variables.instance = StructNew() />

Now some of you may be wondering why you would use the extra struct level of ‘instance’ when we already know everything in that struct is in the components instance (variables) scope.

A quick question to Geoff Bowers gave me a few answers.

  • It’s obvious everywhere you reference this that it is instance data in the variables scope

  • You can dump all the components instance data by using . The benefit of this is that only dumping the variables scope will give you a lot more than just your instance data! Think method signatures etc

  • You can (if you want) separate ‘types’ of instance data. e.g. you might pass in a DAO or Gateway component and assign it to the variables scope (variables.UserDAO)

Another great example of dumping variables.instance quickly and easily (from the Rooibos generator):

1
2
3
4
5
6
7
8
9
10
<!---
DUMP
--->
<cffunction name="dump" access="public" output="true" return="void">
  <cfargument name="abort" type="boolean" default="FALSE" />
  <cfdump var="#variables.instance#" />
  <cfif arguments.abort>
      <cfabort />
  </cfif>
</cffunction>

Comments