David Harris's Technology Blog

ColdFusion, Flex, and other stuff...   (and 323,761 hours, 43 mins in to my plan for global domination)

Search:

Calendar:

Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Subscribe:

Enter your email address to subscribe to this blog.

Archives By Subject:

Tags:

adobe air ajax cfug coldfusion flash flex frameworks free software fxug general jpgmetadatareader mac off topic opensource papervision spry

Recent Entries:

Top Posts:

Recent Comments:

Top Commenters:

My Links:

RSS:


Skeleton Key for a CFC - unlock any CFC!

I was working with the ColdFusion Adminsitrator API today, and had the code:

<cfset oAdmin   = createObject("component","cfide.adminapi.administrator")>

While working with this, I decided to see what was in the "variables" scope of the CFC. Now, as you know the "variables" scope inside a CFC is not available outside of the CFC...right?

Well, no.

Here is a really easy way to get round it.

NB: If you find yourself doing this often, I suspect you are using the CFCs wrong! ;-)

One.CFC Code

<cfcomponent output="false">

   <!--- nicely hidden in the variables scope --->
   <cfset variables.value   = "This Value Is One">


   <!--- need to user getter and setter to update value --->
   <cffunction name="getValue" access="public" output="false" returntype="String">
      
      <cfreturn variables.value>
      
   </cffunction>

   <cffunction name="setValue" access="public" output="false" returntype="void">

      <cfargument name="value" required="true">
      
      <cfset variables.value   = arguments.value>
      
   </cffunction>
   
</cfcomponent>

Calling Code

<!--- Delcare a function --->
<cffunction name="tellMeYourSecrets">
   <cfreturn variables>
</cffunction>

<!--- create your CFC --->
<cfset oCFC   = createObject("component","One")>

<!--- stick your function in to the CFC --->
<cfset oCFC.tellMeYourSecrets = tellMeYourSecrets>

<!--- get the variables scope --->
<cfset stVariables = oCFC.tellMeYourSecrets()>

<!--- dump out the secrets --->
<cfdump var="#stVariables#">

What is also interesting to note that the "variables" scope will be passed by reference, which means if you update values in the return structure, you WILL be updating the values inside the CFC.

...add this code to the bottom:

<!--- Update value (look mum, no setter!) --->
<cfset stVariables.value   = "My New Updated Value">

<cfoutput>#oCFC.getValue()#</cfoutput>

the "getValue" function will return "My New Updated Value". So I have bypassed the getter and setter functions all together!

While this is interesting, I can see no real situation when you would want to do this!

(If you do find yourself doing this, please consult with you nearest Guru why you shouldn't be doing this!)

Even by creating getters and setters and hiding everything in the "variables" scope, it's pretty quick to find your way in to the heart of a CFC!

Files attached if you want to try it out for yourself.

Related Blog Entries

Comments