Using CF9 it is possible to have getter and setters automatically created by using the <cfproperty> tag at the top of the CFC. This is really useful but the problem is that the properties are all stored in the variables scope. The usual approach to storing propery data is to create an 'instance' struct in the variables scope and store the data in there. This then allows you to return a memento of this data at any time for debugging purposes or for quickly changing state.
I like having a getMemento() function in all my CFC's so I can see a snapshot of the data stored in the CFC at any time. I therefore decided to write a new one which would work with the property data when using implicit getters and setters in CF9. I found a quick way to do this using the getMetaData() function.
Here is the code for a getMemento() method which will return a struct of the data currently stored in the CFC and defined by the <cfproperty> tags:
<cffunction name="getMemento" output="false" access="public"
returntype="struct" hint="Default memento dump function - only
works if the CFC is using cfproperty tags">
<cfset var instance = StructNew()>
<cfset var md = getMetaData(this).properties>
<cfset var x = 0>
<cfloop from="1" to="#ArrayLen(md)#" index="x">
<cfset instance[md[x].name] =
variables[md[x].name]>
</cfloop>
<cfreturn instance>
</cffunction>
+