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:

<cfscript>
    //The old way
    var myArray = ArrayNew(1);
    ArrayAppend(myArray, "objec_new");
    ArraySort(myArray, "ASC");

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

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

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

Member functions for ColdFusion data types

Member functions for the following data types are supported:

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:

Member Functions can also be chained, 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.


Fork me on GitHub