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