Creating CFCs

Contact Us or call 1-877-932-8228
Creating CFCs

Creating CFCs

All code within a CFC should be wrapped in a <cfcomponent> tag, which can contain one or more <cffunction> tags. The <cffunction>s are the methods of the component. The only major difference between functions written in CFCs and standard ColdFusion functions is that a CFC function has an access level, which is assigned in the access attribute of the <cffunction> tag.

Access Level Description
private only accessible from other methods in the same component
package only accessible from components in the same directory
public accessible from any ColdFusion page in the application
remote accessible over the internet and by the Flash Player.

The code below shows an example of a CFC that has similar functionality to the imageflipper custom tag we saw earlier.

Code Sample:


	<cffunction name="ImageFlipper" access="public" returntype="string">
		<cfargument name="directory" type="string" required="true">
		<cfargument name="extensions" type="string" default="jpg,gif">
		<cfset aryImages = getFileList(,arguments.extensions)>
		<cfparam name="SESSION.picture" default="0">
		<cfset SESSION.picture = SESSION.picture + 1>

		<cfif SESSION.picture GT ArrayLen(aryImages)>
			<cfset SESSION.picture = 1>
		<cfset imagepath="[SESSION.picture]#">
		<cfreturn imagepath>
---- C O D E   O M I T T E D ----