2

I'm using the CreateObject() method to create an instance of a CFC and then interacting with this newly created 'instance'. I'm doing this because that's how it seems to be done, but I don't understand why we do this.

Why can't we just interact with the CFC directly instead of creating an instance of it?

volume one
  • 6,800
  • 13
  • 67
  • 146
  • This is a perfectly valid question, but did you do a [search](http://stackoverflow.com/search?q=[coldfusion]+createObject+cfinvoke) first? Seems like [this thread](http://stackoverflow.com/questions/4605746/what-is-the-difference-between-using-cfinvoke-and-createobject-to-run-a-componen) answers your question. – Leigh Jul 01 '14 at 13:45
  • I did a search but not with the same search terms. Thanks for this. – volume one Jul 01 '14 at 13:56
  • Yeah, that happens sometimes. Glad the link was useful. – Leigh Jul 01 '14 at 14:08

3 Answers3

5

A CFC is just a file with some code in it, so it makes little sense to suggest "interacting" with it, just the same as you might suggest "interacting" with a CFM file without <cfinclude>-ing it or similar.

A CFC defines a component, and to use a component, one creates an instance of it. In some languages - eg Java - one can have static properties and methods, and one can access them via the class rather than necessarily object, but CFML does not have this concept. CFCs define components which are used as objects, just the same as in other languages a class defines what it is to be an object, and to use an object, one first needs to create an instance of it.

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • I suppose I meant why create an instance when you can call it directly using ``. – volume one Jul 01 '14 at 11:12
  • `` is the same as `CreateObject()` just more OOP-friendly and intuitive syntax – Sergey Babrenok Jul 01 '14 at 11:46
  • 6
    if you're invoking the component more than once on a page you're wasting resources creating it each time. – Matt Busche Jul 01 '14 at 11:59
  • 3
    @volumeone `` still creates an instance, it just discards it straight away after using it. Another benefit of using `createObject()` or `new` is that the code won't look so weird to anyone maintaining the code who might not be so au fait with the idiosyncrasies of some of the ways tag-centric CFML does some of these things. IE: `` is a pretty bloody weird way of calling a method on an object, compared to `myResult = myObj.myMethod()` – Adam Cameron Jul 01 '14 at 14:20
  • @AdamCameron how would you go about submitting a Form to a CFC? Do all forms have to submit back to a CFM template which then does a CreateObject() and submits the Form variables as parameters? At the moment I submit direct to CFC in the action attribute of the Form. – volume one Jul 01 '14 at 22:00
  • It's been four years since I was in control of the code I work on. We use Fusebox 5-ish, and everything goes via .cfm files. Writing my own code? I'd be using a frameworks - prolly FW/1 - which is all CFC-based except for views, I think? But it'd just be a CFC-based controller which'd then assign the work out to various other services to [do whatever] before serving up the view layer implemented via CFMs – Adam Cameron Jul 01 '14 at 22:45
4

You can call the cfc directly using cfinvoke. You just have to realize that cfinvoke creates an object of the cfc first, then executes the method you invoked. Also, once the method is invoked, the object is no longer available.

If your .cfm page is only going to use one method of the component, cfinvoke is ok because there is less code for you to write. However, if you use two or more, it's less efficient because a new object has to be created each time.

In other word, while you don't have to create an instance of the cfc first, it's often a good idea to do so.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
  • how would you go about submitting a Form to a CFC? Do all forms have to submit back to a CFM template which then does a CreateObject() and submits the Form variables as parameters? At the moment I submit direct to CFC in the action attribute of the Form – volume one Jul 01 '14 at 22:00
  • I do not submit forms to cfcs. When receiving form posts I rarely see the need for a cfc. Where I work, we see cfc's as primarily another option to create re-useable code. Other uses are for web services and ajax calls. – Dan Bracuk Jul 01 '14 at 23:26
3

I hope you have read OOPs and its practices. CFC is your 'blueprint' (say a car design) and object is your own data model (say a car of blue color (method to set color), with nitrogen filled tires (method to set pressure in tires) and runs on LPG (method for fuel type)). CF allow you interact directly with CFC (CFINVOKE) and you do not have to create an instance each time but it just only make sense that you would not want to go to workshop/design lab each time you want to change a configuration for your car.

CFML_Developer
  • 1,565
  • 7
  • 18
  • yes I'm reading CF Object Oriented Programming at the moment. I kind of get it. If I'm serving coffee (coffe.cfc), then each person who wants a coffee will get an instance of the coffee.cfc. But each person could also drink from the same coffee which is kind of like telling the applicaton to coffee.cfc each time a request is made. So its an abstraction type thing to not interact directly with the CFC? Is there any significant performance improvement? – volume one Jul 01 '14 at 11:16
  • you are getting there :) except that each person will have own tastes for coffee. Someone wants it without milk, someone with milk, less sugar/more sugar etc. So as far as abstraction is concerned, user is not bothered what process is going behind in kitchen. As @Dan Bracuk said in his answer it will be inefficient to create a new coffee pot each time. Instead fill the pot, serve the coffee according to the need, add milk, add sugar, cream etc. So creating an instance will save you overhead of creating object each time and using single method of it. – CFML_Developer Jul 01 '14 at 12:05
  • To add, CF is not fully OOPs. It still does not adhere to all OOP's principles (polymorphism comes to mind), but it is getting very very closer now. – CFML_Developer Jul 01 '14 at 12:10