DevLearningTools

MODULE 7 · LESSON 05

Properties

Declaring a CFC's data with property / <cfproperty>, and the accessors attribute that auto-generates getX()/setX() methods.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

Properties are a CFC's data — the fields every instance carries its own copy of. Declaring them explicitly (rather than just setting variables.x directly) documents what an object holds and, with accessors enabled, saves writing getter/setter methods by hand.

Learning Objectives

After completing this lesson, you'll be able to:

  • Declare properties with property and <cfproperty>.
  • Use accessors="true" to auto-generate getX()/setX() methods.
  • Set a property's type and default value.

Declaring Properties

CFScript — Product.cfc
component accessors="true" {

    property name="title" type="string";
    property name="price" type="numeric" default="0";

}
Tag Syntax — Product.cfc
<cfcomponent accessors="true">
    <cfproperty name="title" type="string">
    <cfproperty name="price" type="numeric" default="0">
</cfcomponent>

Auto-Generated Getters and Setters

With accessors="true" (the default), ColdFusion generates a getX() and setX() method for every declared property, without writing them by hand.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

setX() methods return void by default and don't return this, so they can't be chained unless you write a custom setter — covered in the Methods lesson.

Property Attributes

AttributePurpose
nameThe property's name — required
typeData type (string, numeric, boolean, array, struct, ...)
defaultA default value used when nothing is explicitly set
hintDocumentation shown when introspecting the component

Common Beginner Mistakes

Expecting getX()/setX() to exist with accessors turned off

If a component (or one it extends) sets accessors="false", the auto-generated getters/setters aren't created — reading/writing has to go through variables.x directly, or custom methods have to be written.

Reading a property directly from outside the component

product.title works only if the property is exposed via the this scope (which accessors do by default) — relying on variables.title directly from outside the component won't work, since variables is private.

Best Practices

  • Always declare a type on properties — it documents intent and helps the auto-generated setter catch mistakes.
  • Use default for a property that should never be undefined, rather than checking for it manually everywhere it's read.

Interview Questions

What does accessors="true" do?

It tells ColdFusion to auto-generate a getX() and setX() method for every property declared on the component, instead of requiring them to be written by hand.

What determines a property's default value if nothing is set?

The default attribute on the property / <cfproperty> declaration.

Summary

In this lesson, you covered declaring properties and using accessors to auto-generate getters and setters instead of writing them by hand.

What's Next?

The next lesson covers methods — writing a CFC's own functions, and the method-chaining pattern of returning this.