Day 15: ColdFusion 10 and CFC Enhancement

Implicit constructor for CFC
ColdFusion10 now support initialize while instantiate the CFC by passing key-value pair or struct. This method will NOT work if your CFC has init() method or initMethod attribute in CFCOMPONENT added. To enable implicit constructor specify accessors=true in cfcomponent or you have setter function for properties defined. If you define setter = false or setter not defined then value for that property will not set instead it will call onMissingMethod defined for CFC.

To test create sample cfm file with below code.
[code:cf]
<cfscript>
emp = new employee({firstname="Pritesh",lastName="Patel"});
</cfscript>
[/code]

Below are different CFC format I have tried.
Test 1:
[code:cf]
<cfcomponent accessors="true">
    <cfproperty name="firstname" type="string" setter="true"/>
    <cfproperty name="lastname" type="string" />   
</cfcomponent>
[/code]
Accessors set to true and both property have setter attribute to true (Default setter value is true) so my object emp has both first name and last name set with my passed arguments.

Test 2:
[code:cf]
<cfcomponent accessors="true">
    <cfproperty name="firstname" type="string" setter="true"/>
    <cfproperty name="lastname" type="string" setter="false"/>   
</cfcomponent>
[/code]
Accessors is true and lastname setter setup with false. In this case only firstname will initialize but lastname will not since ColdFusion may not able to find setlastName method.
Test 3:

[code:cf]
<cfcomponent accessors="false">
    <cfproperty name="firstname" type="string" setter="true"/>
    <cfproperty name="lastname" type="string" setter="true"/>   
</cfcomponent>
[/code]
Accessors is false, no setter/getter will be defined in Component and this will not initialize any value.

Test 4:
[code:cf]
<cfcomponent accessors="false">
    <cfproperty name="firstname" type="string" setter="true"/>
    <cfproperty name="lastname" type="string" setter="true"/>   

<cffunction name="init">
  <cfreturn this>
</cffunction>
</cfcomponent>
[/code]
Accessors is true, setter for both properties set to true but since init method available value will not initialize with passed argument since it will invoke init() method.

Method Chaining for CFC methods:
If you are jQuery fan then you must like this feature :). CFC now support chaining but in case of accessors set to true and method found. To test let’s continue with employee.cfc.

[code:cf]
<cfcomponent accessors="true">
    <cfproperty name="firstname" type="string" setter="true"/>
    <cfproperty name="lastname" type="string" setter="true"/>   

<cffunction name="getFullname" returntype="string">
  <cfreturn firstname & " " & lastname>
</cffunction>
</cfcomponent>
[/code]

You can set firstname and lastname and get full name with below syntax.
[code:cf]
<cfscript>
emp = new employee();
fullname = emp.setFirstName("pritesh").setLastName("patel").getFullName();
</cfscript>
[/code]
Make sure accessors and setter set to true for chaining.

CFC Implicit notation

This new feature allows you to use component property simply like variable for initialize/get value without calling setter/getter. You may need to set invokeImplicitAccessor variable to true in Application.cfc and then you can use ColdFusion property like variable to set/get value.
Error when loading gists from https://gist.github.com/.
<cfscript>
emp = new employee();
emp.firstname="Pritesh";
emp.lastname = "Patel";
fullname = emp.getFullname();
</cfscript>