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 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

Member functions for the following data types are supported:

CF 2016 changed the return type for append member functions:

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:

Numeric member functions

Display and Formatting member functions

Future member functions

These are exclusively member functions and have no headless equivalent. You use them on the Future returned by runAsync.

Standard Future member functions

Empty Future member functions

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