Sam Farmer

Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened.

10 Little Ones: Automatic CFC Init (#5)

April 30, 2012

One of the language enhancements in ColdFusion 10 beta is implicit/automatic constructors for CFC's. In short this means that if a component has defined properties and there is no init function ColdFusion will look to match the parameters passed in to the properties. Lets take a look at how it works: component accessors="true" { property firstname; property lastname; } user = new user( firstname="Homer", lastname="Simpson" ); writeDump( var=user, format="text", showudfs=false );

Which will produce:

[cfc1] component user

Properties: 

	firstname: Homer 

	lastname: Simpson

Parameters can be passed in as name-value pairs or as a struct:

user = new user( {firstname="Homer", lastname="Simpson"} );

2 Comments

Sounds good, as long as it's still possible to override this behaviour using a custom init() method.

By: Michael Zock 05/01/2012 5:12 AM
@Michael: If there is an init function, or one defined by the initMethod attribute on the component, then the implicit/automatic does not happen.

By: Sam Farmer 05/01/2012 7:43 AM
Some comments have been lost over the years due to moving hosts.

More


More blog entries that I have written.

Code coloring by PRISM.