> Hello. Please help me out with a little issue I have. I've recently started
> working with CFC's and there is something that I want to get clear. I have
> two different ways to write CFC's (although I'm sure that there are many
> others). Let me get to the point. The first method uses an init() function
> acts as a constructor.
> <cffunction name="init">
> variables.attributes = structNew();
One thing you could do with your init() method is to have optional
arguments for each of your object properties here too. This allows the
calling code to init() the object and set some properties in one hit,
rather than having to init() then set().
> variables.attributes.cityId = 0;
> <cfreturn this>
Here's a question for you. I see a lot of people doing this, and I just
don't get it. You've got this <cfscript> block, but you stick the return
statement outside it. Why not do this:
variables.attributes.cityId = 0;
It's a very trivial thing, but it piques my interest.
> <cffunction access="public" name="getId" returntype="numeric">
> <cfreturn This.id>
I would say with properties exposed in the THIS scope, one would not
necessarily bother with getters. The whole point of THIS-scoped variables
is that they're accessible directly. Using your example code, one would
just do personDTO.id in the calling code.
That said, I would almost never store my object's properties in the THIS
scope, I'd keep them safely locked away in the VARIABLES scope.