Tuesday, October 24, 2006

Referencing the 'this' scope inside of a CFC


When calling the functions of a CFC it is important to note the difference between the way Java uses the “this” scope and the way ColdFusion references it.

In Java, a call to this.someMethod() makes the request “inside” the instance. Just as if you had simply called someMethod(). Within a CFC a call to this.someFunction() applies the function call from “outside” the object instance as if you had instantiated another instance of the class and are now calling the function on it. (Similar to the way the CFINVOKE tag works.) The key problem with this is that if someFunction() is a private function a call to this.someFuntion() will fail as the caller is not in a part of the same scope. The solution is to never call a local function from inside of a CFC using “this.” as you might be inclined to if you come from a Java background but instead to call the function directly. e.g. x=someFunction() not x=this.someFunction().