Variables in ColdFusion Components (CFCs)


A couple of tips for anyone working with ColdFusion Components, aka CFCs:

  • You can control whether a function/method is accessible outside of the CFC or not by using the access argument on the cffunction line:
    • access="private" will make the function only available within the CFC so it can't be called from outside.
    • access="public" will make the function available to outside pages and other CFCs.
    • access="remote" will make the function available to outside pages, other CFCs and via remote applications through a web services interface (WSDL); you need to use this one if you want to make it available to Flash or Flex applications, etc.
    • access="package" will make the function available to other CFCs that extend it or are in the same code archive (called a "package").
  • You can control how variables are accessed within or from outside the CFC:
    • Any variables you set to the this scope are available outside of the CFC and as a result can conflict with other instances of your CFC! Watch out for this one!
    • If you want a variable local to only the function/method, use the var scope, e.g.
      <cfset var.widget = 'blue' />
      Note that you need to do this for loop counters too.
    • If you want to be able to access the variable from anywhere within the current instance of your CFC use either the variables scope or don't define a scope at all; for accuracy sake I recommend assigning everything to the variables scope if it doesn't fit one of the other criteria above.

Happy coding!

How to reply

Care to add your own 2 cents? Let me know via Twitter or my contact page.