Learning CF9: Implicit Getters and Setters

Posted on October 18, 2009 at 2:50 PM in ColdFusion

One of the (many) really nice features of CF9 is the addition of implicit getters and setters for CFCs. While I love having a concrete API, I am learning to love this idea more and more because of the saved keystrokes.

Housekeeping

Before we go any further, let me go ahead and say it:

Yes, I know that in the purest form, what CF9 calls "implicit getters and setters" is not actually accurate. However, for the sake of simplicity and continuity with the CF9 documentation, I am going to use the term "implicit". If you don't like it, get over it.

What Implicit Getter/Setter Does

According to the CF9 docs...

"By default, properties that you specify by using the <cfproperty> tag have implicit setPropertyName and getPropertyName methods that access the PropertyName property in the CFC variables scope."

Translation: If you have the following...

  1. <cfproperty name="Foo" />

... CF9 will automatically generate the following two methods for you:

  1. <cffunction name="getFoo">
  2. <cfreturn variables.foo />
  3. </cffunction>
  4. <cffunction name="setFoo">
  5. <cfargument name="foo" />
  6. <cfset variables.foo = arguments.foo />
  7. </cffunction>

However, the documentation is wrong. Perhaps "wrong" is too strong a word, but the docs are certainly inaccurate and incomplete. In order to actually get CF9 to behave in this manner, you must also add the 'accessors' attribute to your <cfcomponent> tag.

  1. <cfcomponent accessors="true">

With this in place, CF9 will now generate the get/set{Property} methods for you on properties that you have defined with <cfproperty>.

Comments
(Comment Moderation is enabled. Your comment will not appear until approved.)
CodeBassRadio

Latest Articles

Eventually something really brilliant and witty will appear right here.

Calendar

May 2026
S M T W T F S
« Apr  
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            

Subscribe

Enter a valid email address.

The Obligatory Wish List