This vs Variables Scope in CFC

This is just rehashing of Raymond Camden’s very useful post of Variable Scopes in CFCs.

I always wonder whether to use This or Variables to scope the variables that are available to all the functions on for that particular CFC. This is a problem that I didn’t have before because now I am writing a LOT of CFCs from scratch (CFC is just starting to become a practice in this company). I know I’ve read some explanations from books and blogs but none has been as clear as Ray’s post.

From what I’ve read on the post, variables scoped with This can be accessed outside the CFC via referencing the variables directly, this is similar to declaring your variables public in Java. A no no in OO practice, as you want getters and setters to handle access to variables.

So I’ve got the confirmation today to use Variables scope instead of This.

There are also a couple things worth mentioning from the post:

  • Always use var to scope variables inside functions/methods (admittedly a practice I just picked up recently).
  • Avoid using these scopes inside CFC: Form, URL, Application, Session, Server, CGI, Client, Request, Cookie. As this is not portable.