>> From: "Aaron J. White" >>I consider myself fairly new to Coldfusion and OpenBD. I think one >>thing I really struggle with is breaking out of my linear style of >>coding. I'm self taught and never went to school for specifically for >>development. Coldfusion is great because it gets out of my way and >>lets me jump right into development, but that can also be a curse. >>Anyone know of a good resource to help me understand OO with >>Coldfusion?
I too have come from a self educated background on program. And wrapping yourself around OO and ColdFusion is not that hard. So let me share some of the things I use to keep it all straight in my head. First of all, think of OO as nothing more then linear blocks of code, place on index cards that you can move around and put where you need, and duplicate as often as you like. The key difference here is you are going to use the title of the index card instead of all the code. That being said let me show you pieces from one of my websites I use. The best way to start is to create a CFC file and place it in a folder on your site. I use a folder called CFC myself to keep things organized, and that is how I am going to give this example. For this I will use the name example.cfc . As far as the cfm page is concerned, you do not need to add in anything like you would to access javascript, as the cffunction is called inline in your coding. Just to help distinguish what pages I am working on, I will call my cfm page test.cfm and put it in the root folder on the site. so our file structure is: cfc/example.cfc test.cfm And for those hard core coders out there, I am not going to use method and function and other lingo because studying Java and C++ has got them all jumbled in my head. And if you want to learn the exatct terms, there are books, htis is just something to help the newbies cross hurdles I remember having issues with. Lets start now by looking at the example.cfc page and how you lay out cffunctions to be read like index cards: start off by setting up your page with <cfcomponent> </cfcomponent> then within the tags, you place your index cards: <cfcomponent> <cffunction name="theIndexCard" access="public" returntype="array" output="no"> </cffunction> </cfcomponent> This is basically what is needed to hold the block of linear code you are going to run. Take note of the access="public". That needs to be set to public. If you set it to private you will get an error. And the name is important as well as this is the title of the index card and will be used on your test.cfm page. returntype is needed more to control what your index card can return, which helps to avoid someone trying to crack your database. Withing the cffunction tag, it is best to go ahead and put in your cfarguments. These are the variables you are going to hand the index card to run through your linear coding. <cfcomponent> <cffunction name="theIndexCard" access="public" returntype="array" output="no"> <cfargument name="firstName" required="yes" type="text"> <cfargument name="lastName" required="yes" type="text"> </cffunction> </cfcomponent> Looking at the tags, name is the name of the variable, required is to tell the index card if it can process without it, and type is recommended to help keep people from cracking your database, as is using the cfparam tag along with it anywhere someone is using a text field to enter data. And finally to wrap up the example.cfc I will put in a little database call and return the database <cfcomponent> <cffunction name="theIndexCard" access="public" returntype="array" output="no"> <cfargument name="firstName" required="yes" type="text"> <cfargument name="lastName" required="yes" type="text"> <cfquery name="qPerson" datasource="#Session.source#" username="#Session.name#" password="#Session.thisPass#"> SELECT ID FROM persons WHERE firstN = "#arguments.firstName#" AND lastN = "#arguments.lastName# " </cfquery> </cffunction> </cfcomponent> Just to briefly cover this, when you use the cfargument tag, it is best (not required, just helps keep things readable) to use arguments.cfargument name to reference the variable. One thing to note is when you add more cffunctions to the example.cfc you do not add more <cfcomponent> tags, you just continue to place within them: <cfcomponent> <cffunction name="theIndexCard" access="public" returntype="array" output="no"> <cfargument name="firstName" required="yes" type="text"> <cfargument name="lastName" required="yes" type="text"> <cfquery name="qPerson" datasource="#Session.source#" username="#Session.name#" password="#Session.thisPass#"> SELECT ID FROM persons WHERE firstN = "#arguments.firstName#" AND lastN = "#arguments.lastName#" </cfquery> <cfreturn = qPerson > </cffunction> <cffunction name="anotherFunction" ...... </cffunction> </cfcomponent> Now to look at the test.cfm and how to use the index card we just created. The call to the index card can be done pretty much anywhere you can put coldfusion code at. And for this example it will be in the <body></body> of the webpage. To call the index card we use: <cfinvoke></cfinvoke> tags as follows: <cfinvoke component="cfc.example" method="theIndexCard" returnvariable="thePersonsID" > </cfinvoke> the component="cfc.example" tag is basically the directory you have it in, and the name of the cfc you are trying to access. Remember I placed my example.cfc in a folder called "cfc" and the "/" for the directory is replaced with a "." and you remove the extention of of the file name so "example.cfc" becomes "example". Putting it all together you get "cfc.example" To elaborate a step further if I put my example.cfc file in a directory called indexCards instead of cfc it would be: "indexCards.example". Or if I put it 2 levels deep "cfc/indexCards/example.cfc" the proper address would be "cfc.indexCards.example" the method="theIndexCard" is the name of the cffunction we are using in the example.cfc the returnvariable="thePersonsID" is the name we want to call the answer we are getting back. This can be any valid name, aka returnvariable="myReturn". Within the cfinvoke tags you place the arguments you are going to pass to the index card: <cfinvoke component="cfc.example" method="theIndexCard" returnvariable="thePersonsID" > <cfinvokeargument name="firstName" value="#Session.First#"/> <cfinvokeargument name="lastName" value="#Session.Last#"/> </cfinvoke> Notice that name="firstName" and name="lastName" are the same as the cfargument tag names in the example.cfc. They must match, otherwise you will get an error. with the value="" tag I am using a session variable from a previous page to supply the information needed to the card, it can be anything you want to pass to the index card you are calling . At this point, anyplace after the cfinvoke tag has been called you can use the information returned in the returnvariable. So in this case I would say put <cfoutput> Your Current ID is: #thePersonID# </cfoutput> so a completed setup would be: example.cfc <cfcomponent> <cffunction name="theIndexCard" access="public" returntype="array" output="no"> <cfargument name="firstName" required="yes" type="text"> <cfargument name="lastName" required="yes" type="text"> <cfquery name="qPerson" datasource="#Session.source#" username="#Session.name#" password="#Session.thisPass#"> SELECT ID FROM persons WHERE firstN = "#arguments.firstName#" AND lastN = "#arguments.lastName#" </cfquery> <cfreturn = qPerson > </cffunction> <cffunction name="anotherFunction" ...... </cffunction> </cfcomponent> test.cfm <body> <cfinvoke component="cfc.example" method="theIndexCard" returnvariable="thePersonsID" > <cfinvokeargument name="firstName" value="#Session.First#"/> <cfinvokeargument name="lastName" value="#Session.Last#"/> </cfinvoke> <cfoutput> Your Current ID is: #thePersonID# </cfoutput> </body> There are a few things to keep in mind when doing OO in coldfusion. All variables you use are within the scope of the function. AKA you can't access a <cfset> set in one cffunction tag in another cffunction tag unless you use a Session Variable. And when you do that, it is a good idea to wrap them in a <cflock></cflock>. -- online documentation: http://openbd.org/manual/ google+ hints/tips: https://plus.google.com/115990347459711259462 http://groups.google.com/group/openbd?hl=en Join us @ http://www.OpenCFsummit.org/ Dallas, Feb 2012
