Instantiating CFCs (Hello World!)

There are so many different ways to instantiate CFCs, that I decided that I would document them here for those who are interested.

Here is the helloWorld.cfc that we’ll be working with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<cfcomponent hint="Hello World Component">
	<cffunction name="greet">
		<cfreturn "Hello World!">
	</cffunction>
 
	<cffunction name="greetUser">
		<cfargument name="name" default="">
		<cfset greeting = "Hello #arguments.name#!">
 
		<cfreturn greeting>
	</cffunction>
 
 	<cffunction name="display">
		<cfargument name="name" default="">
		<cfset greeting = "Hello #arguments.name#!">
		<cfoutput>#greeting#</cfoutput>
	</cffunction>
 
</cfcomponent>

<cfinvoke> is the most common way that you are able to instantiate a
CFC, so we’ll start there.

First things first, we’ll set a variable that we’ll use later on:

1
<cfset firstName="Jim">

 

Next we’ll instantiate our ‘helloWorld’ CFC using cfinvoke and output the result:

1
2
<cfinvoke component="helloWorld" method="greet" returnvariable="greetMe">
<cfoutput>#greetMe#</cfoutput>

Pretty simple, right? What we’ve done here, is we’ve instantiated the component ‘helloWorld’, and called the method ‘greet’. That method returns a value to the variable ‘greetMe’, and that is sent to the display using <cfoutput>.

ok… so what if you want to pass an argument to the component? Using cfinvoke, there are 3 ways that you can do this.

The first method is to include the arguments inline within cfinvoke:

1
2
<cfinvoke component="helloWorld" method="greet" firstname="#firstName#" returnvariable="greetMe">
<cfoutput>#greetMe#</cfoutput>

 

note the ‘firstname’ argument being passed into the CFC. or you can use <cfinvokeargument>,
which is really handy if you have a lot of arguments that you are passing at
one time, as including your arguments inline is sometimes a little hard to read:

1
2
3
<cfinvoke component="helloWorld" method="greet" returnvariable="greetMe">
 <cfinvokeargument value="#firstName#" name="firstName">
</cfinvoke>

 

The final method for instantiating a CFC with arguments is to send them as
a structure using argumentCollection:

1
2
3
4
<cfset args="StructNew()">
<cfset args.firstName="#firstName#">
<cfinvoke argumentCollection="#args#" component="helloWorld" method="greet" returnvariable="greetMe">
<cfoutput>#greetMe#</cfoutput>

 

Now that we understand using cfinvoke… let’s look at the second method for
instantiating CFCs, using cfobject:

1
2
3
<cfobject component="helloWorld" name="myObject">
<cfset greetMe="myObject.greet()">
<cfoutput>#greetMe#</cfoutput>

 

cfobject is not supported by most shared hosting providers, but if you are
not developing for a shared hosting environment, you may prefer to use it, as
the syntax is closer to the way that other languages work with objects.

as an alternative to cfobject, you can use createObject(), which is the function
equivalent of the cfobject tag (this is also not available within most shared
hosting environments):

1
2
3
<cfset myObject="createObject("component", "helloWorld")">
<cfset greetMe="myObject.greet()"> 
<cfoutput>#greetMe#</cfoutput>

You are also able to use createObject from within cfscript. This my preferred
method:

1
2
3
4
5
<cfscript>
myObject = createObject("component", "helloWorld"); 
greetMe = myObject.greet();
</cfscript> 
<cfoutput>#greetMe#</cfoutput>

 

Also keep in mind that one of the benefits of working with CFCs is that once
they are instantiated, there is no need to repeat ourselves… and we can access
anything within that object… in the following examples, we won’t instantiate
the CFC. Here’s an example of passing arguments to a CFC using cfset:

1
2
<cfset greetMe="myObject.greetUser()">
<cfoutput>#greetMe#</cfoutput>

 

and another example using cfscript:

1
2
3
4
<cfscript>
greetMe = myObject.greetUser("#firstName#");
</cfscript> 
<cfoutput>#greetMe#</cfoutput>

 

and while it is really recommended that you keep your display logic out of
your CFCs, you are able to return that cfoutput from within the CFC itself if
you have a method to do it:

1
2
3
<cfscript>
greetMe = myObject.display("#firstName#");
</cfscript>

 

note that in our helloWorld.cfc above, the ‘display’ method contains the cfoutput
that we’ve been using in the rest of this article… so when it is called, it’s
job is to display the greeting, so there is no actual ‘return’. this is not
the best MVC practice, but it is possible… and so there you have it.

My good friend Matt Turner also offered another alternative to createObject()
for use within a shared hosting environment… he’s built a component called
‘object.cfc’ (which you could name anything you want really):

 

 

1
2
3
4
5
6
7
8
9
10
<cfcomponent hint="replacement for cfobject / createObject()">
	<cffunction name="object">
		<cfargument name="type" default="component">
		<cfargument name="component" required="true">
		<cfargument name="method" required="true" default="init">
		<cfset var object=""/>
		<cfinvoke component="#arguments.component#" method="#arguments.method#" returnvariable="object"></cfinvoke>
		<cfreturn object/>
	</cffunction>
</cfcomponent>

 

this gets instantiated within your application.cfc onRequestStart() using cfinvoke:

1
<cfinvoke component="object" method="init" returnvariable="create">

 

and then we instantiate our object into the variables scope:

1
2
3
<cfscript>
myObject = create.object("component","helloWorld");
</cfscript>

 

Using this method, you could replace all your calls to createObject or cfobject…
and still use the same syntax for dealing with your objects without the conflicts
found in shared hosting environments. Thanks Matt!

 

 

 

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Pownce
  • Technorati
  • TwitThis
  • De.lirio.us
  • E-mail this story to a friend!
  • Fark
  • LinkedIn
  • Slashdot
  • Socialogs
  • StumbleUpon

2 Responses to “Instantiating CFCs (Hello World!)”

  1. paolo Says:

    Hi,
    I try to implement the last tip about replacement for cfobject / createObject() but …
    I am a beginner with cfc and maybe I make some great mistakes… Could you give me a light? thanks
    I have the files structure:
    Application.cfc, object.cfc, helloworldservice.cfc (here I would like to use the replacement)
    ———
    application.cfc

    ————–

    object.cfc

    —————-
    helloworldService.cfc


    —————-
    I get the error:

    The method ‘init’ could not be found in component C:\CFusionMX7\wwwroot\helloworld\object.cfc.

  2. paolo Says:

    hi, I have fix it adding a init method to object.cfc

    and modifying the cfinvoke to set the result to a variable in the request scope (to use it also in customtag and component)

    I don’t know if it is correct .. anyway, thanks for the hint that make me solve the issue of cfobject replacement on shared hosting…

Leave a Reply

You must be logged in to post a comment.