Edit

ColdFusion Member Functions

CF 11+ Member Functions allow variables of certain CFML data types to be treated as objects with functions. Invoking a member function on the object (the variable) allows you to skip passing the variable into the function because it already has a reference to the value. The use of member functions often lead to more concise and readable code.

For instance, consider the following headless function:

arrayAppend(empArr, emp)

Using a member function it can be written as:

empArr.append(emp)

Where empArr is a reference to an instance of a CFArray class (possibly a variable created with arrayNew).

The following example depicts the new usage of member functions:

// The standard way
var myArray = arrayNew(1);
arrayAppend(myArray, "objec_new");
arraySort(myArray, "ASC");

// The member way
myArray.append("objec_new");
myArray.sort("ASC");

// The member way
var myProductObject = createObject("java", "myJavaclass");
myjavaList = myProductObject.getProductList();
myjavaList.add("newProduct"); // Java API

myjavaList.append("newProduct"); // CF API
myjavaList.sort("ASC");

Member functions for the following data types are supported:

CF 2016 changed the return type for append member functions: anystruct.append() returns the appended structure anyarray.append() returns the appended array

Array member functions

String member functions

List member functions

Struct member functions

Date member functions

Image member functions

Spreadsheet member functions

XML member functions

Query member functions

When using Query.cfc, you get your results from the execution by using:

Display and Formatting member functions

Numeric member functions

Future member functions

Empty Future member functions

Member Functions can also be chained (on Lucee or CF 2018+), for example:

s = "the";
s = s.listAppend("quick brown fox", " ")
     .listAppend("jumps over the lazy dog", " ")
     .uCase()
     .reverse();

result: GOD YZAL EHT REVO SPMUJ XOF NWORB KCIUQ EHT

Important Note on a potential Member Function Gotcha

Some member functions might fall into underlying Java methods if the strict ColdFusion syntax is not followed.

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub