Accessibility

MAX 2003 Article

Object-Oriented ColdFusion

Simon Slooten

Simon Slooten

http://www.prisma-it.com/

This article is an excerpt from Simon Slooten's session (SS310-W) at the Macromedia MAX 2003 conference. For sneak peeks of Flash and Dreamweaver sessions, see Art Phillps' article, Consuming Web Services with Macromedia Flash MX Professional 2004 and Betsy Bruce's article, Tableless Layout Using the CSS Box Method. Don't miss out on these sessions or any of MAX 2003! Register now for this year's MAX conference in Salt Lake City November 18-21 . If you can't make it, be sure to check out the live conference coverage during the conference on the MAX Developer Center.

Why would someone from Holland sit in a plane for about 14 hours or so to travel from Rotterdam , Holland to Salt Lake City , Utah in the middle of November? Ask this question to an ‘outsider' and they would say that you are bonkers to do such a thing. Ask it to an ‘insider' and they would go “Wow, you're going to the Macromedia MAX? Cool!”.

The hottest frenzy of meeting old friends, business peers, making new friends and an unbelievable supply of information on the latest web technologies and new, cool, and useful products will be in Salt Lake City this year: It's Macromedia MAX 2003!

This year there will be 90 sessions and workshops on all experience levels and fields of expertise about development, design, and Macromedia products.

I'm honored to be able to give you a sneak preview on my session this year: Object Oriented ColdFusion (SS310-W) .

ColdFusion is a procedural language, and that's one of reasons it is so unbelievably easy to start using. But if you have been programming in ColdFusion for some time and maybe have looked at other languages, chances are that you've looked at an object-oriented language (OO language).

With object-oriented languages, you look at the "problem" in a different way than with procedural languages. In OO languages you build stand-alone blocks of code that manage their own data. Keep this code and the data it manages in objects .

In a procedural language it's more likely to develop in a top-to-bottom approach. With a scalable application, however, you rely on code that promotes reuse, readability, and is easy to use for modeling. And that is exactly what OO languages are about. It's probably not a coincidence that there are few procedural languages in the industry.

So, does that make ColdFusion the ‘odd one out’? No, certainly not, for two reasons (and many more than that):

  • Procedural is still a very easy way of coding. Whether you’re a starter or an experienced ColdFusion developer, there’s nothing wrong with procedural.
  • With ColdFusion MX, you can actually code in a more OO way with ColdFusion layered on top of the procedural language.

If you want to switch from a procedural brain to an object-oriented brain, this session is for you.

We'll be looking at the typical OO slang, such as: classes, instances, inheritance, encapsulation, polymorphism, and so forth, and how ColdFusion MX implements these elements.

In ColdFusion MX, object-oriented programming (OOP) begins with ColdFusion Components (CFCs). During the session we will talk about using CFCs to separate your code (static CFCs) by building objects that contain methods. We will also look at building real objects from a CFC (dynamic CFCs) complete with properties and methods.

Let's look at that now to give you an example from my session:

An object (or instance) is a collection of data (properties) and a set of actions (methods) that can manipulate that data. An object exists in server memory and is created from a Class. In ColdFusion the Class is the CFC.

You can create an object from a CFC with the <cfobject> tag or the createObject() function:

  <cfobject 
 	component="myApp.components.User"
  name="userObject"> 

Or:

	<cfset userObject=createObject("component", "myApp.components.User")> 

The object (including its methods) is now in server memory. Use the <cfdump> tag to confirm the object you created.

You can invoke the object's method(s) with the following code:

	<cfinvoke
	component="#userObject#"
	method="getAllUsers"
	returnvariable="qGetAllUsers">

You see, it’s almost the same as calling a ColdFusion component.

But you can also invoke the object with the following:

<cfset qGetAllUsers=userObject.getAllUsers()>

This produces the same result with less code, and this looks more like OOP.

You can place objects in persistent scopes too, such as the application or server scope; then you can use them throughout your whole application. How, you ask?

You'll learn during this session!

The session also looks at all the special ColdFusion scopes and keywords that you will use in object-oriented programming in ColdFusion. The "This" scope, the "var" keyword, the "super" keyword, and the "Arguments" scope. Let's not forget inheritance and polymorphism. We'll cover it all.

You want another example? Let's look at inheritance:

Inheritance in ColdFusion extends a parent object.

If you have a user object:

<cfcomponent>
	<cffunction name="getAllUsers" returntype="query">
		<cfset var qGetAllUsers="">
		<cfquery name="qGetAllUsers" datasource="dsn">
			SELECT * FROM Users
		</cfquery>
		<cfreturn qGetAllUsers>
	</cffunction>
</cfcomponent>

This user object retrieves all the user data (username, password, role, and so forth) for the application users. A user is a special sort of contact , which is also an object in our application, a contact object . The contact object holds data for all contacts (name, email, and so forth). Not all contacts are users in our application.

In creating a user object , it should also have all the properties and methods of the contact object. The user object should inherit data from the contact object.

Does that sound difficult? Wait till you see the code. Let's take the same user object you just created:

<cfcomponent extends="contact">
	<cffunction name="getAllUsers" returntype="query">
		<cfset var qGetAllUsers="">
		<cfquery name="qGetAllUsers" datasource="dsn">
			SELECT * FROM Users
		</cfquery>
		<cfreturn qGetAllUsers>
	</cffunction>
</cfcomponent>

See the extends attribute in the <cfcomponent> tag? That's all you have to do!

Now your user object has inherited all the properties and methods from the contact object. So if the contact object had a getAllContacts() method, your user object now also has a getAllContacts() method.

OK, one more tip. With the super keyword you can access properties of the parent of an object. One of the limitations of the super keyword is that it only works one level up, or does it? Is there a way to access methods of parent objects more than one level up the tree?

Yes, there is a way to do that. How to do it? You'll learn at the session!

During the session, we will also look at one or two things to keep in mind when working with Flash Remoting MX and CFCs.

The questions we'll answer during the session:

  • Does Flash MX know about my ColdFusion session?
  • What about my persistent objects? Can I use them with Flash Remoting?
  • But...I still have this old ColdFusion application. Can I use that with Flash Remoting?

If you want to learn all about applying basic OO ideas and concepts to ColdFusion development, don't miss this session or on the MAX conference. I look forward to meeting you.

My personal experiences from previous Macromedia developer conferences have always been so great that I really look forward to the next event every year. Even if attending that event means sitting in a plane for 14 hours, it's worth it.

See you in Salt Lake City !


About the author

Simon Slooten is founder and CEO of Prisma IT , a Macromedia Alliance Partner (I SV, Solution Provider, Hosting & Training) based in Rotterdam , Holland.

Since 1997, he has been one of only a handful of Macromedia Certified Master Instructors in the world and he is a Macromedia Certified Advanced ColdFusion MX, Flash MX and Dreamweaver MX developer.

Simon has over 20 years of international experience in the computer industry in software development, consultancy and training. He started using the internet when he worked for a Digital Equipment (DEC) partner in 1985.

When he first saw ColdFusion in 1996, he was immediately sold and has been using the product ever since. He is a regular speaker and has spoken at Macromedia/Allaire Developer Conferences in 1999, 2000 and 2001, CF-Europe, Flashtival, and many other international events.