Add Comment
CF can do OO Tutorial
ColdFusion Tutorial #5
This is a basic example of OO in CF
demo.cfm
The example creates 2 objects. Book and Library. Heres what it does.Line 1: Create a library.
Lines 3-8: Retrieve some data from the sample database.
Lines 10: Loop over the data from the query. For each book take the data and create a new book object setting its properties in the constructor. ( no it’s not a real constructor but it’s the CF way ) Add each book to the library.
Lines: 15 - 24: Call the listLibrary function which will return a pipe separated list of the book details. And insert it into the grid.
I have dumped out the objects to show that the properties are protected and the only way to change or even see the values is via its methods.
book.cfc
The Book object is nothing more than a struct which has its variables protected. The only way to retrieve or modify the variables is via its methods.The Book object has the methods getTitle(), getAuthor() and getDescription(), these functions return a string, setTitle(), setAuthor() and setDescription() these functions require a string.
library.cfc
The Library object is a container for books. In this example it will store an array of Book objects. But it could also, for example store a list of cd’s. The library class cannot directly change the properties of a book without calling the appropriate methods.A Library object has addItem() which take a object of type book and listLibraryTitles() which returns a list of items.
Demo
See this code running!
Download
Download this code as a zip!
Comments
Contstructor is spelled "Constructor".Lines 32, 38 and 44 should read "set" instead of "return".
anItem should be "an Item"
Phillip Senn @ Tuesday 04 Sep 2007 - 12:03:32 AM
Thankyou,
This has been fixed.
Dale Fraser @ Tuesday 04 Sep 2007 - 10:32:14 AM
For OOP beginners, don't try to learn OOP in CF entirely from blogs and tutorials. Read "head first: Object oriented Analysis and Design" to get your head wrapped around the concepts. All of this stuff makes so much more sense then. It's a heck of a lot easier to apply this coldfusion specific stuff when you actually understand the fundamentals.
justin @ Wednesday 05 Sep 2007 - 05:43:20 AM
And I think one should take the time to learn the fundamentals of the tags before trying OO as well.
For instance, I'm reading the cf8 manuals now, learning how to bind inputs, use the new tags, etc.
I want to make sure I'm using what's native to cf before branching out to writing my own Javascript, framework, etc.
Phillip Senn @ Wednesday 05 Sep 2007 - 06:09:31 AM
in book.cfc, shouldn't the setTitle method and other set methods use arguments.newTitle instead of arguments.title?
i.e.
<cfset variables.title = arguments.newTitle />
instead of
<cfset variables.title = arguments.title />
eapen @ Saturday 08 Sep 2007 - 03:24:03 AM
Yes you right I have fixed it :) Because I didn't call the function in the example I didn't notice this.
Paul Kukiel @ Saturday 08 Sep 2007 - 09:20:35 PM
Click button to add a comment
Author
Paul Kukiel