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:


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

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


<cfcomponent extends="">
<cffunction name="init" access="remote">
<cfinvoke method="setter" varName="" varValue="bar" />
<cfinvoke method="getter" varName=""
returnVariable="baz" />
<cflog text="#baz#" />

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

Comments are closed.