Using Variables

Variables can be created within CFML a variety of different ways. Apart from the simple types, there are complex types supported at a language level.

Simple Values

You can create new variables by simply using the tag CFSET and assigning them to a new variable name.

<cfset a = 1>
<cfset b = true>
<cfset c = now()>
<cfset d = "StringValue">

Creating Arrays

An array is a list of simple values, complex types or objects that are referenced using the indexed they occupy in the list. Array's in CFML are 1 (one) based and not 0 (zero) based like Javascript or Java.

There are mainly two ways to create arrays. The classic approach is to use the ArrayNew() function, passing in an optional dimension element.

<cfset myArray = ArrayNew()>
<cfset myTwoDimensionalArray = ArrayNew(2)>

Once created, you can address elements using the [] notation, with it creating extra space in the array accordingly. For example the following will add two elements to the array, blanking out the elements in between that have not been addressed.

<cfset myArray = ArrayNew()>
<cfset myArray[1] = "Alan">
<cfset myArray[5] = "Ceri">

Alternatively you can create arrays and initialize them at the same time using implicit creation.

<!--- create an empty array --->
<cfset myarray = []>

<!--- create an array with 3 numeric elements --->
<cfset myarray = [1,2,3]>

<!--- create an array with mixed element types --->
<cfset myarray = [true,'andy',3]>

<!--- create an array with mixed element types; carriage returns are ignored --->
<cfset myarray = [

<!--- create an array of arrays --->
<cfset myArray = [ [1,2], [3] ]>

Creating Structures

A structure (sometimes referred to as a map or a collection) is a collection of data, arranged in key/data pairs. A structure can only have unique keys and data is accessed by specifying the key. The majority of CFML scopes are structures (form, server, request etc) and can be accessed using the key notation.

Creating a structure can be done using the classic approach of calling the StructNew() function. Once created you can use the [] or the . (dot) notation to get and set data.

<cfset myStruct = StructNew()>
<cfset myStruct["name"] = "Alan">
<cfset myStruct.location = "Scotland">
<cfset myStruct.created = now()>

Alternatively, you can create and set data in a structure using implicit creation. Note OpenBD supports the Javascript method of creating structures, using the : (colon) delimiter.

<!--- create an empty struct --->
<cfset myStruct = {}>

<!--- create a struct with fields, using CFML syntax --->
<cfset myStruct = {
  name = "Andy",
  country = "Scotland" }>

<!--- create a struct with fields, using Javascript syntax --->
<cfset myStruct = {
  name : "Andy",
  country : "Scotland" }>

<!--- create structs within a struct --->
<cfset myStruct={ a.c = 1, a.b.c = 3 }>

<!--- Mixed example --->
people = [ { name = 'Andy', country = 'Scotland' },
           { name = 'Matt', country = 'USA' },
           { name = 'Alan', country = 'Scotland' },
           { name = 'John', country = 'Australia' } ];

scottishCount = 0;
for ( i = 1; i < arraylen(people); i++ ){
  if ( people[i].country == 'Scotland' ){

Creating Objects

There is a more Java like way of creating your objects. Instead of using CreateObject(), you can now use the new Object() syntax, in much the same way you would declare objects in Java.

In addition to this, the unofficial/official constructor for CFC's, the init() function, is also called at object creation time, if found. Which incidentally means, that the init() function no longer needs to return back "this" which is the standard pattern in CFC creation at the moment. Naturally, you can still do that with no side effects.

The import statement defines the packages/paths that will be searched for the CFC when you are using the "new" operator.

import "org.openbluedragon.importexample";
import org.openbluedragon.importexample;

obj = new Test();
obj = new Test( arg1, arg2 );
obj = new org.openbluedragon.importexample.Test();

Some advanced examples:

<!--- simple cfc path --->
<cfset obj = new test()>

<!--- full cfc path --->
<cfset obj = new>

<!--- cfc path as string --->
<cfset obj = new "test"()>

<!--- full cfc path as string --->

<cfset obj = new ""()>

<!--- with arguments --->
<cfset t = new test( "foo", "bar" )>

<!--- with named arguments --->
<cfset t = new test( bar: "bar", foo: "foo")>

<!--- with args passed in as argumentcollection --->
<cfset args = { bar = 2, foo = 1 }>

<cfset t = new test( argumentcollection: args )>