On Dec 19, 2003, at 1:41 PM, Ben Curtis wrote:
I'm just stretching my legs, trying to accumulate some techniques that may or may not prove useful someday. I bumped into a way for private functions to be publicly available and I can think of a couple uses (which may or may not be useful), but before I go and try them out I wanted to see if this is a designed "feature" and likely to stick around in future versions, or a bug likely to disappear.

Well, I can't say that it is a "designed" feature but it is a consequence of the way CFCs are implemented or rather how CF functions are implemented. A CF function is implemented as a class so you get an instance of the "function object" as a member of the CFC (the CFC is also compiled to a class). So, functions are really just objects (data) that you can call methods on (and there's a method that is called to execute a "call" to that function object). If that sounds confusing, don't worry - you mostly don't need to know.


Anyway, the access specifier is not really inherent in the function object - it just determines whether the (reference to the) function object is stored in "this" scope (access="public") or "variables" scope (access="private"). Once the function reference is stored in one of those scopes, that determines how you access it. Clearly, you can't access data in the "variables" scope from outside the component (which is why you can't call "private" functions from outside the CFC). If you copy the reference to the "this" scope, then you make the function public and you can call it from outside the CFC.

It's the same implementation 'trick' that lets you add UDFs to CFC instances:

<cfscript>
function hijack() {
        return variables;
}
obj = createObject("component","mycfcs.foo");
obj.sneaky = hijack;
objvar = obj.sneaky();
</cfscript>

Now objvar has a reference to the non-public "variables" scope defined in the mycfcs.foo instance (because ColdFusion has "dynamic binding" of variable names - the 'variables' inside 'hijack' is looked up only when the function is called... and of course it finds the 'variables' scope defined in the CFC instance!).

I wouldn't recommend writing code that relies on this technique.

Sean A Corfield -- http://www.corfield.org/blog/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email.


CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]

Reply via email to