Snippets: Extending the Generic Getter (and the hardGet() method)
Lets start with a simple User object where you'd like to add a full name attribute based on first name and last name properties. You'd simply add a custom getFullName() method to your custom user object (which extends the base object) with the following code:
<cfscript>
var Local = StructNew();
Local.ReturnValue = variables.hardGet('FirstName') & " " & variables.hardGet('LastName');
</cfscript>
<cfreturn Local.ReturnValue>
</cffunction>
Note that getFullName() is private. The interface to getters is the generic get(AttributeName) getter which handles the process of determining whether to use a default or custom getter, so as a best practice in this architecture, all custom getters (and setters) should be private.
You also need to make sure that the FullName is one of the "gettable" attribtues in the custom user init() pseudo constructor:
<cfscript>
Super.Init();
variables.gettableAttributes = "FirstName,LastName,FullName,Age";
</cfscript>
<cfreturn This>
</cffunction>
When a script calls User.get('FullName'), providing the attribute is "gettable" (a metadata setting in the user init() function), the generic getter will look for a getFullName() method. Finding it, it will call that method and return that value to the calling script. (If there wasn't such a script it would just use the generic hardGet(AttributeName) method described below).
As for the hardGet() method, it just gets a value from an array of structs based on the current row within the recordset and the name of the property being requested. Naturally hardGet() is a private method as it is only available to the generic getter and any custom getters.
<cfargument name="AttributeName" type="string" required="yes" hint="The name of the attribute to get">
<cfset var Local = StructNew()>
<cftry>
<cfscript>
// Just pull the attribute
Local.ReturnValue = variables.Data[variables.IteratorRecord][AttributeName];
</cfscript>
<cfcatch>
<cfif variables.ReturnNullforNonExistantAttributes>
<cfset Local.ReturnValue = "">
<cfelse>
<cfset Local.ReturnValue = "NON_EXISTANT_ATTRIBUTENAME(#arguments.AttributeName#)">
</cfif>
</cfcatch>
</cftry>
<cfreturn Local.ReturnValue>
</cffunction>


