ColdFusion 9 getters, setters and Constructors

One of the the things that I really wished that ColdFusion 9 did was that its constructors could be self aware of what was being passed into it.

From a coding point of view it would have made the language a bit more RAD than it is now.

ColdFusion 9 has some awesome new features in the scripting side of things, and the fact that it also now has the concept of a constructor under the hood makes our jobs just a bit more easier than it used too.

However there is one down fall to this.

When you write code like this.

myObj = new cfcObject(id:'someID', title: 'This is a title');

Doesn't do anything at all. So if you have getters and setters enabled, then you will find that your code above will not use this magic under the hood.

One way around this and thanks to Ray Camden for pointing out the error I was making, we can do this with little code. It would be nice if ColdFusion could do this auto magically for us, but for now it doesn't and its not too late to show your support over at the Adobe bugs database to ask for it to be included.

Anyway this is how we can do it with very little code in our constructor.

function init() {
for(item in arguments) {
evaluate("set#item#(arguments[item])");
}
}

Fairly simple to do, but not very RAD.