components

kjerk edited this page Oct 17, 2013 · 6 revisions

Previous Variables --- Next Strings


## 3. Components, Methods, and Parameters ### Components (aka Objects or Classes)

In CFML, a ColdFusion component (CFC) is a file that contains data and methods. Components are the building blocks for objects in CFML. Objects know information, called "attributes", and can do actions, called "methods". In ColdFusion the cffunction tag is used to define methods within a tag-based CFC. In a script-based CFC, you use the function keyword to define a method.

For an example of an object, think about you as a human being. You have attributes like height, weight, and eye color. You have methods like walk, run, wash dishes, and daydream. Different kinds of objects have different attributes and methods. In the next sections we'll look at a few specific instructions in CFML.

In CFML we define an object using the cfcomponent instruction (component in script-based CFCs) and save the file as .cfc. Here's an example defining the object type PersonalChef.cfc:

Tag Syntax

<cfcomponent>

</cfcomponent>

Script Syntax

component {

}

Methods

Inside the CFC we usually define one or more methods using the cffunction or function instruction like this:

Tag Syntax

<cfcomponent name = "PersonalChef">
    <cffunction name = "makeToast">
        <cfset var makeToast = "Making your toast!" />
    </cffunction>
</cfcomponent>

Script Syntax

component name = "PersonalChef" {
    public void function makeToast() {
        var makeToast = "Making your toast!";
    }
}

Inside the cffunction/function instruction we would put the code for how the chef should make the toast.

Classes

A "class" is an abstract idea, it defines what all objects of that type can know and do. Think of the chair you're sitting in. It’s not an abstract chair, it is an actual chair. While a "Chair" class would represent a chair in the abstract sense, we would call the actual chair an "instance" of the "Chair" class. It is a realization of the idea of the Chair. It has measurable attributes like height, color, and weight. The class Chair, on the other hand, is abstract. It is abstract in the sense that the class's attributes, such as height, color, and weight, cannot be determined ahead of time.

Once we define a class, we create an instance of that class like this:

Tag Syntax

<cfset frank = New PersonalChef() />

Script Syntax

frank = New PersonalChef();

We're calling the New instruction on the class PersonalChef and storing it into the variable named frank. Once we have the instance, we can set or get its attributes and call its methods. Methods are called by using this syntax: object.method_name(). So if you have a person named "frank" you would tell him to make toast by calling frank.makeToast().

The New instruction creates a new instance of the object and calls its init() method (if existing). Any arguments supplied to the object will be passed to the init() method. The init() method should return the object instance using <cfreturn this /> in order to have the same expected behavior as the CreateObject instruction. If no init() method exists, the object will be returned normally.

Method Parameters

Sometimes methods take one or more parameters telling them how to do what they're supposed to do. For instance, I might call frank.makeToast("burned") for him to burn my toast. Or maybe he has another method where I call frank.makebreakfast("toast","eggs") for him to make both toast and eggs. Parameters can be numbers, strings, or any kind of object. When a method takes a parameter we use the cfargument instruction, it'll look like this:

Tag Syntax

<cfcomponent name = "PersonalChef">
    <cffunction name = "makeToast" returnType = "void">
        <cfargument name = "color" required = "yes">
        <cfset var makeToast = "Making your toast #arguments.color#!" />
    </cffunction>
</cfcomponent>

Script Syntax

component name = "PersonalChef" {
    public void function makeToast(required String color) {
        var makeToast = "Making your toast #arguments.color#!";
    }
}

The method is requiring us to pass in a "color" telling it how to do the method "makeToast".

Return Value

In CFML, every time you call a method you won't necessarily get a value back. By default, a CFML method returns nothing. We'll talk about nothing and "null" in the last section of "CFML in 100 minutes". If you called makeToast method above like <cfset result = frank.makeToast("burned") /> or result = frank.makeToast("burned");, and tried to output result you should have seen "Variable RESULT is undefined".

To return data, we use cfreturn to instruct the method to return a "value". Since that wasn't in the last instruction before the ending cffunction in your makeToast method, you received nothing and tried to putting that into the "result" variable.

For the purposes of our next section I’m going to return the chef instance itself from the method. If you wanted to picture the metaphor, imagine you are looking at your chef "frank". You say, "Frank, go make my toast", he tells you he's making the toast, goes to make it, then comes back to you to receive more instructions. He's returning himself to you. Here's how we implement it in code:

Tag Syntax

<cfcomponent name = "PersonalChef">
    <cffunction name = "makeToast" returnType = "component">
        <cfargument name = "color" required="yes">
        
        <cfset this.makeToast = "Making your toast #arguments.color#!" />
        <cfreturn this />
    </cffunction>
</cfcomponent>

Script Syntax

component name = "PersonalChef" {
    public component function makeToast(required String color) {
        this.makeToast = "Making your toast #arguments.color#!";
        return this;
    }
}

Previous Variables --- Next Strings