The Dopefly Tech Blog

<< The Dopefly Tech Blog Main page

Fun with Functions and CFCs IV - Public Method, Different from the Private Method

posted under category: ColdFusion on December 13, 2007 at 1:00 am by MrNate

Here's an interesting idea. Probably not a good one, but check it out. What if external calls to a CFC method execute something different than when you call that method from within the CFC? It's easy. Here's the code:


  <cffunction name="init" access="public">
    <cfreturn this>

  <cffunction name="privateMethod" access="private">
    <cfreturn "This is a Private Method!">

  <cffunction name="publicMethod" access="public">
    <cfreturn "This is a Public Method!">

  <cfset this.genericMethod = publicMethod />
  <cfset variables.genericMethod = privateMethod />


Now private calls to genericMethod() will give you the private method message, and public calls to myCFC.genericMethod() will return the public method message.

I honestly can not think of a use case for this one. Got any? leave a comment.

Too old to comment!
On Dec 28, 2007 at 1:00 AM Adrian Lynch ( said:
Another level of security maybe?

You might have a level of trust in calls from within the CFC and mistrust from external calls.

Using this you can keep the API the same.

On Jun 5, 2008 at 1:00 AM ike ( said:
If you wanted to log a particular type of activity. Let's say for example that you have a component where you can set a particular value and internally the component needs to do some extra leg-work when it's set, but it sets it somewhat frequently from the internal... However there are cases in which some other external object wants to set that value and you want to log when that happens so you can review it later but you don't want the log full of all the internal events. You could wrap the internal method with an external method that calls it and logs the activity and result. That's just off the top of my head - and I realize there are of course other ways to skin that cat.
Too old to comment!