Tim Blair

Private CFC methods and "this" (01/09/2003)

One common misconception in CFMX developers who started life in the Java world is that CFCs work the same as java objects. This of course is not true, but the changes can sometimes be very subtle.

An example of this can be found in the use of the "this" scope. In Java, a call to this.myMethod() makes the request "inside" the instance. Within a CFC the behviour is different: a call to this.myFunction() applies the function call from "outside" the instance, as if you had instantiated the object externally and are now calling the function on it.

The problem with this difference is that if you have a private function in a CFC and call that method from within the CFC (as you would) then if you call this.myFunction() you will recieve a function-not-found error, exactly the same as you would from attempting to call a private method from outside the CFC.

The same is also true using package-access functions when the calling script is outside the "package" of the CFC. If the CFC and the calling CFM page are in the same directory then using this.myFunction() will succeed; if the calling script is not in the same package then it will fail as above.

Article Archive (September '03 – '05)