How To Never Have to Write Another Get or Set Method Again

If you don’t need your setters and getters to do anything more than just set and get properties (without modifying those properties), you might be able to save yourself the hassle of implementing all those set and get functions by building a little support framework. In the example below, there is a component called “support.cfc” which all components that need a lot of get and set methods should extend. Notice how the component “test_support.cfc” is free to set and get arbitrary properties without those set and get methods actually having been implemented. (This will also hold true if your component contains an instance of a component that extends support as opposed to extending support itself as in the example below.) I believe you can even scope variables (with or without “this”) using this technique:

support.cfc

<cfcomponent>
<cffunction name="setter" access="public">
<cfargument name="varName" type="string" required="yes" />
<cfargument name="varValue" required="yes" />
<cfscript>
setVariable(varName, varValue);
</cfscript>
</cffunction>

<cffunction name="getter" access="public">
<cfargument name="varName" type="string" required="yes" />
<cfreturn evaluate(varName) />
</cffunction>
</cfcomponent>

support_test.cfc

<cfcomponent extends="com.macromedia.util.support">
<cffunction name="init" access="remote">
<cfinvoke method="setter" varName="this.foo" varValue="bar" />
<cfinvoke method="getter" varName="this.foo"
returnVariable="baz" />
<cflog text="#baz#" />
</cffunction>
</cfcomponent>

I’m sure there are better ways to implement this type of technique, but you get the idea.