Book Image

Object-Oriented Programming in ColdFusion

By : Matthew Gifford
Book Image

Object-Oriented Programming in ColdFusion

By: Matthew Gifford

Overview of this book

Are you tired of procedural programming or is your extensive code base starting to become un-manageable? Breathe some new life into your code and improve your development skills with the basic concepts of object-oriented programming. Utilize objects, modular components, and design patterns to expand your skills and improve your ColdFusion applications. Packed with example code, and written in a friendly, easy-to-read style, this book is just what you need if you are serious about ColdFusion.This book is a fast-paced tutorial to important ColdFusion object-oriented programming topics. It will give you clear, concise, and practical guidance to take you from the basics of ColdFusion to the skills that will make you a ColdFusion developer to be reckoned with. Don't be put off by jargon or complex diagrams; read and see how you can benefit from this book and extend your development skills in the process.Using the practical examples within this guide, you will learn how to structure your applications and code, applying the fundamental basics of object-oriented programming to develop modular, reusable components that will scale easily with your application. You will learn the basic fundamental practices of object-oriented programming, from object creation and re-use, to Bean objects, service layers, Data Access objects, and sample design patterns to gain a better understanding of OOP using examples that can be altered and applied in your application. Complete with detailed code samples and snippets, and written in a friendly easy-to-follow style, you will be able to break free from writing purely procedural code and enhance your applications by building structured applications utilizing basic design patterns and object-oriented principles.
Table of Contents (14 chapters)
Object-Oriented Programming in ColdFusion
Credits
Foreword
About the Author
Acknowledgement
About the Reviewer
Preface

Accessing your CFC


Once you are ready to use your CFCs, you need to access the methods placed within. There are two ways to access a CFC:

  • object instantiation

  • invoking the CFC

Instantiating the object

When you instantiate a CFC, you create an instance of the component. This instance preserves the data within the CFC for as long as it exists. You would typically create an instance of a component at the top of the page. By doing so, you would have access to its methods and functions on the entire page, without having to create a new instance for each call.

There are three methods available to you to create an instance of the CFC:

  • createObject

  • cfobject

  • using the NEW operator in ColdFusion 9

Using the createObject function

As used in our earlier examples, the createObject() function creates and returns a ColdFusion object.

<cfscript>
objGreeting = createObject('component', 'greetings');
</cfscript>

Listing 1.19

Here, we are creating a new instance of the "greetings" component. The first parameter tells the createObject() function that we want a component, and the second references the name of the component of which we want to create an instance.

While the CFC is in the same directory as the calling page, in the previous example, if we had the CFC within a different directory in the webroot, for example, a folder named components; this second parameter would read as follows:

createObject('component', 'components.greetings');

This is because the second parameter is a dot notation representation of the path to the component.

Using the cfobject tag

Similar to the createObject() function, the cfobject tag has three attributes.

<cfobject name="greetingsObject" component="greetings" type="component" />

Listing 1.20

The name attribute defines the name of the returned variable of the CFC instance so you can access the component to use your methods. The component attribute represents a dot notation path to the CFC you wish to instantiate. The third attribute, type, is optional, and has the default value component.

Using the NEW operator

The enhancements in ColdFusion 9 now provide an alternative way of creating an instance of a component object without using the createObject() function.

We can now create the object through the use of the new operator, like so:

<cfscript>
// create the object
objGreeting = new greeting();
</cfscript>

Listing 1.21

Using cfinvoke

You can invoke your component and access the method simultaneously by using the cfinvoke tag. When you invoke (call) the CFC using this tag, you are not creating an instance of the component that will be preserved and available for use elsewhere within your CFML page. Instead, you are creating an instance of the CFC that comes into existence as soon as you invoke the method, and ceases to exist as soon as the requested method has returned a result.

In essence, you are bringing the component to life long enough to get the details you need from it and closing it down as soon as the information is returned.

The cfinvoke tag

Let's make a call to our sayHello() method within the greetings.cfc component.

Add the following code to your hello.cfm template page:

<cfinvoke component="greetings" method="sayHello" returnVariable="strHello" />
<cfoutput>#strHello#</cfoutput>

Listing 1.22

Here, we are invoking the greetings component, selecting the method within the CFC that we want to access, (in this case the sayHello() function) and assigning a variable (strHello), to which the returned data will be saved for us to access it within the page.

Outputting the returnVariable onto the page will provide us with the same result as we have seen before.

Using cfinvokeargument

The cfinvoke tag also allows us to pass in parameters to the methods we are calling, by means of the cfinvokeargument tag.

<cfinvoke component="greetings" method="personalGreeting" returnVariable="strPersonalGreeting">
<cfinvokeargument name="firstName" value="Matt" />
<cfinvokeargument name="lastName" value="James" />
</cfinvoke>

Listing 1.23

We are sending our parameters used in the personalGreeting() method in a similar format to the cfargument tag, using the cfinvokeargument tag, which is nested within the cfinvoke tag. The cfinvokeargument tag takes the name and value of the argument and sends it into the method you are calling.

Using attributes as arguments

Alternatively, when using the cfinvoke tag, you can send through the parameters as named attribute-value pairs, providing one attribute per argument.

<cfinvoke component="greetings" method="personalGreeting" firstName="Gary" lastName="Brown" returnVariable="strPersonalGreeting" />

Listing 1.24

You can see in the previous code that the firstName and lastName parameters are written as attributes within the cfinvoke tag itself.

Using an argument collection

The optional argumentCollection attribute for the cfinvoke tag accepts a structure in the form of an associative array of arguments to pass into the method.

<cfscript>
// create a structure to hold the values
stuArguments = structNew();
stuArguments.firstName = "James";
stuArguments.lastName = "Brown";
</cfscript>
<cfinvoke component="greetings" method="personalGreeting" argumentCollection="#stuArguments#" returnVariable="strPersonalGreeting" />

Listing 1.25

The structure names must match the names of the arguments within the method.