A CFC is a single .cfm-like file, saved with a .cfc extension, whose entire contents are wrapped in a component definition. Nothing lives outside it — no loose code before or after.
Learning Objectives
After completing this lesson, you'll be able to:
- Write a CFC in both CFScript (component) and tag (<cfcomponent>) syntax.
- Use the key attributes: extends, implements, accessors, output.
- Know the basic file rules a .cfc has to follow.
CFScript: the component Keyword
component accessors="true" {
property name="name" type="string";
property name="age" type="numeric";
function init(required string name, numeric age = 0) {
variables.name = arguments.name;
variables.age = arguments.age;
return this;
}
}Tag Syntax: <cfcomponent>
<cfcomponent accessors="true">
<cfproperty name="name" type="string">
<cfproperty name="age" type="numeric">
<cffunction name="init" returnType="User">
<cfargument name="name" type="string" required="true">
<cfargument name="age" type="numeric" required="false" default="0">
<cfset variables.name = arguments.name>
<cfset variables.age = arguments.age>
<cfreturn this>
</cffunction>
</cfcomponent>Key cfcomponent / component Attributes
| Attribute | Purpose |
|---|---|
| extends | Name of a parent component to inherit methods and properties from |
| implements | One or more interfaces this component must satisfy (comma-delimited for multiple) |
| accessors | Whether ColdFusion auto-generates getX()/setX() methods for declared properties (default true) |
| output | Whether the component's pseudo-constructor code (outside any function) can produce HTML output |
| persistent | Marks the CFC persistent for Hibernate ORM use |
| hint / displayname | Documentation shown when introspecting the component |
File Rules
- The file extension must be .cfc, and the filename becomes the component's default name when instantiated.
- The entire file must be wrapped in the component definition — no code allowed outside it.
- Everything between the opening brace/tag and the first function runs automatically on instantiation — this is the pseudo-constructor, covered in the Constructor lesson.
Common Beginner Mistakes
Writing code outside the component block
A .cfc file's entire content has to live inside the component { } (or <cfcomponent></cfcomponent>) definition — anything else is a syntax error.
Forgetting accessors="true" and then calling a getter/setter that doesn't exist
Auto-generated getX()/setX() methods only exist when accessors is enabled (it defaults to true in modern CFML, but older code sometimes turns it off) — check the component's attribute if a getter call fails unexpectedly.
Best Practices
- Name the CFC file to match what it represents (User.cfc, OrderService.cfc) — the filename is the component's default name everywhere it's referenced.
- Declare property types explicitly — it documents intent and helps catch mistakes when accessors generate getters/setters.
Interview Questions
What does the implements attribute do on a component?
It declares one or more interfaces the component must satisfy — every method signature in the listed interface(s) must be implemented, or ColdFusion raises an error.
What determines a CFC's default component name?
The filename, minus the .cfc extension — User.cfc becomes User when instantiated with new User().
Summary
In this lesson, you covered the component / <cfcomponent> syntax, the key attributes (extends, implements, accessors, output), and the basic rules a .cfc file has to follow.
What's Next?
The next lesson covers actually creating objects from a CFC — new vs createObject(), and what an instance actually is at runtime.