Object-Oriented Blog Process - VI

Code explained.

In post IV I promised to explain some code. So here we go. Notice that in this explanation I'll use the code from the application example from post V.

I'm just going over the OO specific code here. The other parts (as the form field, the css and other html) I will asume that you know from before.

First explanation goes for the following line in index.cfm which is called if the form for creating a new product object is submited.

<cfset session.products[CreateUUID()] = createObject("component","product").init(form.productname,form.productprice) />

Here we use the createObject() function for creating a new object, telling that the object is of the type "component" (a cfc file) and the class file is product (product.cfc without the file extention). For now we are going to keep all the cfm and cfc files in the same folder, so this is all we need to conect to the class file for creating the object. Then we are calling the init() method of the newly created object, and supplying the arguments productname and productprice from the form.

We also see that the object is created as a member of the session.product structure, with a UUID as the object name.

Let us go to the product.cfc

A cfc file should always be opened and closed with a cfcomponent tag set. Next we are setting a structure for holding all the property values for the object. Then the first really interesting part - the init() method.

A method in a cfc is surrounded by starting and ending cffunction tags.

The init() method is the ColdFusion equalent of what in traditional/other OO languages are called the constructor method. This method is used to initiate the initial values of the object we create.

The cfargument tags tells us what properties that can be populated, and also if requred is set to "true" - there must be arguments passed on to populate them.

Then it takes the arguments passed on (or the default value from the cfargument tags) and calls the setter methods of the object to populate it's properties.

The init() method is present in a object by default, and is usually not counted in when we talk about the methods of an object.

Next we come to the "real" methods of the object. In our example so far we only have present so called setter and getter methods for the properties. The setters are used to set the value of the properties, first by the init() method, but if access is set to public they can also be used to update/change the value of the property later on, by being called from the outside (a cfm template or another object). The getter methods are simmply there to expose the values of the properties to the outside (as we'll see soon).

Back to index.cfm

The last part of code to look at is this:

<cfloop collection="#session.products#" item="i">
<div><strong>#session.products[i].getProductName()#</strong> - $#session.products[i].getProductPrice()#<br /></div>
</cfloop>

Here we loop over the session.products struct, which is where we created our objects. Then for each object we loop over we call the getter methods that I mentioned above to get the name and the price of each product.

That is basicaly all magic it takes to create a simple OO based application. I hope everything so far is pretty clear. If not please feel free to contact me (#coldfusion on dalnet is a good place to catch me). In the next number I plan to start talking about inheritance.

Comments
So there is at least one person following the OOP process. Over the past few months, I've been trying to get a better understanding of OOP. I'm with you so far. Not to jump ahead and I'm sure you'll get to it, but I am most interested when and how the Products get committed to a database.
# Posted By Matt W | 11/7/05 4:45 PM
Hi Matt. I'm happy to see someone is following my humble atempt to learn and explain OO in a way that "normal" CF developers can understand. Like you say, I'll get to persisting data in a OO application at some point. Not sure when and how yet. I've pretty much worked out in my head what to write in the next 2 - 3 posts, just need to find the time to actually write it.
# Posted By Trond Ulseth | 11/9/05 9:23 AM