Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between using cfinvoke and createObject to run a component function?

In my company's code, I've often seen component files used by initializing an object of that component and calling the methods off the object. However, it seems to me somewhat more straightforward to use the cfinvoke method, especially when only using one method from the component file. What are the differences between these 2 methods of calling a component function and what are the pros/cons of each? When should I use which?

like image 692
froadie Avatar asked Jan 05 '11 15:01

froadie


1 Answers

One other benefit of using createObject() is that you can chain the init() method, e.g.

<cfset myObject = createObject("com.path.MyObject").init() />

And if your init() returns this you can go further and chain the method if you don't need to use the object again:

<cfset functionResults = createObject("com.path.MyObject").init().myFunction() />

It's worth pointing out that in CF 9 you can use the new (ahem) new syntax to create objects. For example to create the same object as above and call it's init() I can write:

<cfset myObject = new com.path.MyObject() />

It's neat and I like the option to do this. CF is moving in the right direction in my opinion with features like this.

like image 107
Ciaran Archer Avatar answered Sep 25 '22 18:09

Ciaran Archer