Method chaining for CFC methods

You can chain your CFC methods as follows:

emp=new employee();
emp.setFirstName("Tom").setLastName("Nash").setAge("30");
emp=new employee(); emp.setFirstName("Tom").setLastName("Nash").setAge("30");
emp=new employee(); 
emp.setFirstName("Tom").setLastName("Nash").setAge("30");

Chaining works only

  • If the attribute accessors is set to true
  • Until a method is found
  • If there are no errors
    or
  • If the setter functions for the properties are defined

Example

The chain in the example works only until lastName. This is because setter for age is set to false:

<cfcomponent accessors="TRUE">
<cfproperty name="firstname" type="string" setter="true"/>
<cfproperty name="lastname" type="string" setter="true"/>
<cfproperty name="age" type="numeric" setter="false"/>
<cffunction name="init">
<cfreturn this>
</cffunction>
</cfcomponent>
<cfcomponent accessors="TRUE"> <cfproperty name="firstname" type="string" setter="true"/> <cfproperty name="lastname" type="string" setter="true"/> <cfproperty name="age" type="numeric" setter="false"/> <cffunction name="init"> <cfreturn this> </cffunction> </cfcomponent>
<cfcomponent accessors="TRUE"> 
<cfproperty name="firstname" type="string" setter="true"/> 
<cfproperty name="lastname" type="string" setter="true"/> 
<cfproperty name="age" type="numeric" setter="false"/> 
<cffunction name="init"> 
<cfreturn this> 
</cffunction> 
</cfcomponent>

Dapatkan bantuan dengan lebih pantas dan mudah

Pengguna baharu?