Skip to content

Instantly share code, notes, and snippets.

@christophervigliotti
Last active April 20, 2018 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophervigliotti/ee332ccf14fa63a617a95b9f03d7137c to your computer and use it in GitHub Desktop.
Save christophervigliotti/ee332ccf14fa63a617a95b9f03d7137c to your computer and use it in GitHub Desktop.
A brief meditation on encapsulation in ColdFusion
<!---
encapsulation is good stuff https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)
--->
<cfcomponent>
<cfset variables.dsn = '' />
<cffunction name="init">
<cfargument name="dsn" type="string" required="true" />
<cfset variables.dsn = arguments.dsn />
<cfreturn this />
</cffunction>
<cffunction name="sad_stuff">
<!--- referring to the request scope inside of a cfc breaks encapsulation --->
<cfquery name="aQuery" datasource="#request.dsn#">
...
</cffunction>
<cffunction name="good_stuff">
<!--- getting the datasource name from the object's properties respects encapsulation --->
<cfquery name="aQuery" datasource="#variables.dsn#">
...
</cffunction>
<cffunction name="also_good_stuff">
<!--- getting the datasource name from the arguments scope also respects encapsulation, but is less efficient --->
<cfargument name="dsn" type="string" required="true" />
<cfquery name="aQuery" datasource="#arguments.dsn#">
...
</cffunction>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment