There is one thing I really like about Flex classes.
They have real properties (AKA Instance Variables). ColdFusion doesn't have properties in the way that Java or .NET (or ActionScript) do. It often feels like a limitation.
This Variables Scope
If we were to create a CFC, there are two ways you can create instance variables. One approach is to use the 'this' scope. The second is to do so in the 'variables' scope. Take a look at this CFC:
this.foo = "bar";
variables.bar = "foo"
We created 'foo' in the this scope and bar in the variables scope. What is the difference between the two? Let's find out. First we need an instance of this CFC:
MyObject = Createobject('component','MyCFC');
Let's experiment with the foo variable to see what it can do. We can access it using object property notation, like this:
MyObject.foo = 123;
But, what if the component is expecting Foo to be a string and not a number? This direct access change it's type. We probably screwed up some internal process that will cause trouble later. There is no way to protect CFCs instance variable in the this scope. However, if you do this:
MyObject.bar = 123;
You won't affect the value of the private CFC instance variable bar. That's a good thing. It's inherently protected. But, what is bad is that there is no way to access the value of the bar variable from outside the component. The solution in the CF Community has been to write custom get and set methods. These methods are good because we have information hiding and other benefits of encapsulation. It's bad because it's tedious to write numerous get and set methods. Instead of using:
MyObject.bar = 123;
We would use:
To change the value of the instance variable. In the set method we could even add error checking to make sure that the value stays as a string, or number, or whatever.
Public and Private in Flex
Flex has a better way to handle instance variables. I think that will be more familiar to developer's of Java or .NET. You create instance variables in an ActionSCript class for use in Flex. When you do so you specify whether this variable is public or private. The code might look like this:
public var foo = 'bar';
private var bar = 'foo';
Public variables can be accessed just like they were in the this scope of a CFC. Private variables operate just like they were in the variables scope of a CFC. So, what's the difference other than some syntactic sugar?
ActionScript is strongly typed, so you don't have to worry about people setting your public variables to use an unexpected type. You can't change a string to an integer, or an integer to a class. You're inherently protected.
Because of this built in protection, you don't usually end up creating get and set methods. Private variables are created because they are internal the component and don't need outside access. You rarely have to write those tedious get and set methods.
I wish ColdFusion would provide a way to truly define public and private variables within a CFC.