It is certainly expected.  "Correct" is probably a more complicated question
;)

A CFC seems to behave more like a "bucket-o-functions" than like a true
object.  You can "strip" off functions and even "attach" functions.  In
fact, you can attach functions created in the calling page (or wherever)
that return "private" data from the CFC and it will work.

The attached example is from my CFC seminar (the code was inspired by
something Sean Corfield posted to his blog) that shows how you can attach
methods to a CFC instance (and access otherwise "private" data).

This ability is really handy when you need to call a method dynamically --
whether that's "correct" I'll leave for interpretation:

foo = createObject("component","foo");
methodName = "doStuff";
foo["tempMethod"] = foo[methodName];
foo.tempMethod();

In the above, the last line will call the "doStuff" method.

One last thing I'll mention about this: when you "detach" a method from a
CFC instance that method no longer has access to instance data (or public
"this" data for that matter) of the original instance.  That is, if you
reference a "this" variable or a private (unscoped) variable inside the
method, it will act on whatever CFC instance it is attached to (in this way
you could actually create a new CFC on the fly and still operate on private
data -- but, again, that is not necessarily "proper" or a "good thing").




> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Tim Blair
> Sent: Friday, July 25, 2003 2:18 AM
> To: [EMAIL PROTECTED]
> Subject: [CFCDev] References to CFC methods
>
>
> In just trying out the createobject(...).init(...) method of using a
> constuctor, I accidentally missed the brackets off a method call.  I
> expected this to throw an error, but no...
>
> contructortest.cfc:
> -------------------
> <cfcomponent>
>   <cfset instance = structnew()>
>
>   <cffunction name="init">
>     <cfargument name="arg1">
>     <cfargument name="arg2">
>     <cfset instance.arg1 = arguments.arg1>
>     <cfset instance.arg2 = arguments.arg2>
>     <cfreturn this>
>   </cffunction>
>
>   <cffunction name="getinstancevars">
>     <cfreturn instance>
>   </cffunction>
>
>   <cffunction name="somestaticmethod">
>     <cfargument name="arg1">
>     <cfargument name="arg2">
>     <cfreturn arg1 + arg2>
>   </cffunction>
> </cfcomponent>
>
> constructortest.cfm:
> --------------------
> <cfset myobj = createobject("component", "contructortest").init("1",
> "2")>
> <cfdump var="#myobj#">
> <cfdump var="#myobj.getinstancevars()#">
>
> <cfset funcref = myobj.somestaticmethod>
> <cfdump var="#funcref#">
> <cfdump var="#funcref(123, 312)#">
>
> ----------------------------------------------------------------------
>
> Check out the second section in the cfm file -- you can grab a reference
> directly to a method within a CFC and then use that method outside the
> CFC.  I tried it using the getinstancevars() method but got a "Variable
> INSTANCE is undefined" error, so obviously this is being used outside
> the scope of the CFC.
>
> Is this correct (and expected) behaviour?
>
> Tim.
>
>

Attachment: private_not_private.cfm
Description: Binary data

Attachment: secretkeeper.cfc
Description: Binary data

Reply via email to