Part 2: Understanding and Using CFCs (with OOP and database interaction)
Untitled Document

Understanding and Using CFCs (with OOP and database interaction)

Part 2

The basics of a CFC

CFCs are much like regular ColdFusion pages, you can use all of the same tags and functions you would normally use in an application page. However, there are some differences to note, a few which we will go over now.

1) CFCs must have the extension .cfc instead of .cfm or cfml. This tells the ColdFusion server that the page is a component rather than a standard CFM page.

2) The name of the .cfc file is important, as it is how you will call your CFC. It should not contain any periods.

3) CFCs must contain the <cfcomponent> tag. We will go over this later.

4) CFCs contain (but not requried) <cffunction> tags to define the methods (or functions) within that component. We will go into this later as well

Creating our first CFC

The first CFC we are going to create is our job component. We start by creating the file job.cfc in our application directory.

Now lets create the skeleton of our CFC by adding the following code:

<cfcomponent
displayname="Job Component"
hint="This component handles all job functions and database calls">

<!--- we will place our components code here --->

</cfcomponent>

The display name attribute is simply for your reference to what this component is. The name you will use to actually call your CFC from your .cfm page is determined by the .cfc file name, in this instance "job".

The hint attribute is again information for reference for yourself or other developers that may interact with this component.

Initializing the data properties our CFC will have

Think of our job CFC as a single job object, with generic properties. Each job we create will share common basic elements. Each job will have a name. Each job will have a unique database key. Each job will have one or more tasks. Each job needs the ability to be inserted into the database, and so forth.

We are going to define these common data elements in an initialization function. This function goes inside our <cfcomponent> function.

<cffunction name="init">
<!--- initialize data for component --->

</cffunction>

The <cffunction> tag creates a method (or function) within our CFC. The only required attribute is the name attribute. This name is how you will call this function in your code.

Now lets define our data elements inside our function we just created. Notice we are prefixing them with the scope this.

<cffunction name="init">
     <!--- initialize data for component --->
       <cfparam name="this.id" default="">
     <cfparam name="this.name" default="">

</cffunction>

The this scope refers to the current instance of the object. In otherwords, you can create instances of the job component and each of them can have completely different data values for the variables inside them.

By using the this scope, we are letting cold fusion know that those variables we are defining are instance variables, and creating them with default values of null for later.

Calling our initialization function when the component is created

Each time we create an instance of our job component, we want the server to instanciate our data properties. We may also want our component to do other tasks upon creation.

We can achieve this by writing code inside the <cfcomponent> tag, but outside of any functions. In this particular case, we are going to call our init() method we have just created, whenever a new instance of our job component is created.

To do this, we place the following after the open of our <cfcomponent> tag.

<cfscript>
     // call our internal initialize function
     init();
</cfscript>

Now, when this component is created, the first thing that will happen is a call to the internal function init() . This function will then set up our default properties for the component, or our data elements.

Complete code for part 2

Each time we create an instance of our job component, we want the server to instanciate our data properties. We may also want our component to do other tasks upon creation.

<cfcomponent
displayname="Job Component"
hint="This component handles all job functions and database calls">

<cfscript>
     // call our internal initialize function
     init();
</cfscript>

<cffunction name="init">
     <!--- initialize data for component --->
       <cfparam name="this.id" default="">
     <cfparam name="this.name" default="">

</cffunction>

</cfcomponent>

In part three, we will begin tying our component to the database.



All ColdFusion Tutorials By Author: Nate Nielsen