Skip to main content
Inspiring
April 1, 2008
Question

Creating Objects from CFCs

  • April 1, 2008
  • 3 replies
  • 1540 views
I am working on creating a couple of components that will act as objects in the Application and Session scopes. I have been looking at examples online and have notices a couple of different things that I assume are accomplishing the same goal, but wanted to ask someone.

I understand that the THIS scope is used within the object to store variables. I guess my questions more along the lines of how to initialize those variables.

One method I have seen used is just by declaring at the beginning of the component (as below):
<cfcomponent>
<cfset THIS.var1="lala">
<cfset THIS.var2="toto">
<cffunction....

The other method I have seen is using the method init():
<cfcomponent>
<cffunction name="init" ...>
<cfset THIS.var1="lala">
<cfset THIS.var2="toto">
</cffunction>

I am assuming that they are accomplishing the same task.

If someone can explain the differences between these two methods and if there are benefits to either one it would be greatly appreciated. Thanks!
This topic has been closed for replies.

3 replies

Inspiring
April 4, 2008
Actually there are good reasons for both methods of setting variables.

The first method, setting the variables outside of any functions, is used when there will NEVER (or very very nearly never) be a need to change them. Of course these could and should be set during the application initialization and the components just pull from there. BUT for portability you may want to put the data inside.

The init() allows you to create different instances of the same component, like maybe "product" and you pass in the specs on that instance.
Inspiring
April 1, 2008
jeby wrote:

> I am assuming that they are accomplishing the same task.
>
> If someone can explain the differences between these two methods and if there
> are benefits to either one it would be greatly appreciated. Thanks!

Nearly but there is a subtle difference. Code, such as these initial
variable sets inside the <cfcomponent...> block, but NOT inside a
<cfunction...> block, is a ColdFusion Component's version of a
constructor. This code is always run when a component is instantiated.
But you may notice that there is no way to pass arguments to this code
when it is run at the time of creation. The ColdFusion community has
developed the 'init' function as a defacto standard to create a pseudo
constructor to which one can pass arguments.

The init function is not a true constructor, it is not automatically
called during creation of the object, rather it must be implicitly
called by the developer. Thus in your example, the code in the
<cfcomponent...> block would automatically be run every time an object
is created. The init function would only be run when told to by the
developer during run time.

This is often done with a daisy chain command so that the actions follow
one, two. I.E. myObj =
createObject("component","path.to.my.component").init()

Again the reason for using this is to pass in arguments, so one would
usually see code like this:

<cfcomponent>
<cffunction name="init" ...>
<cfargument name="var1"...>
<cfargument name="var2"...>

<cfset THIS.var1=arguments.var1>
<cfset THIS.var2=arguments.var2>
</cffunction>
</cfcomponent>

----------

myObj = createObject("component","my.component").init("lala","toto")

PS. Also must developers would put these values into the 'VARIABLES'
scope rather then the 'THIS' scope. The 'Variables' scope is private to
the component|object, the 'This' scope, on the other hand, is public.

HTH
Ian



Inspiring
April 1, 2008
quote:

Originally posted by: Newsgroup User
PS. Also must developers would put these values into the 'VARIABLES'
scope rather then the 'THIS' scope. The 'Variables' scope is private to
the component|object, the 'This' scope, on the other hand, is public.

HTH
Ian



Public in what way? If they are not returned by any function, how could they be accessed from the calling template?
Inspiring
April 2, 2008
quote:

Originally posted by: Dan Bracuk
Public in what way? If they are not returned by any function, how could they be accessed from the calling template?



The THIS scope is public by nature. So unlike the variables scope it can be accessed, and modified, directly by the calling template.

Participating Frequently
April 1, 2008
I think the following link has a good sample. :)

http://cfoop.org/readArticle.cfm?aid=3