ColdFusion 9's Implicit Getters And Setters Are Hard Coded

Posted June 3, 2010 at 8:46 AM by Ben Nadel

Tags: ColdFusion

The other day, I was day dreaming about the awesomeness that is ColdFusion 9 when I had a thought about its new implicit getters and setters. I know that ColdFusion 9 added the function, getFunctionCalledName(); so, I wondered if the implicit getter and setter methods were actually shared functions that operated based on their run-time handles. To test this, I set up a very simple ColdFusion component that had two properties:

Girl.cfc

  • <cfcomponent
  • output="false"
  • accessors="true"
  • hint="I am a test component with implicit getters.">
  •  
  •  
  • <!--- Define properties. --->
  •  
  • <!--- This will have both accessors. --->
  • <cfproperty
  • name="name"
  • type="string"
  • />
  •  
  • <!--- This will have only a setter. --->
  • <cfproperty
  • name="petName"
  • type="string"
  • getter="false"
  • />
  •  
  •  
  • <!---
  • Set some default values using the implicit setters
  • defined by the above CFProperty tags.
  • --->
  • <cfset this.setName( "Tricia" ) />
  • <cfset this.setPetName( "Cutie" ) />
  •  
  • </cfcomponent>

Notice that both properties of this Girl - name and petName - have implicit setter methods; but, only the name property has a getter. This will cause the petName property of this object to be unreadable. So the question is, if I copy the getName() function reference into the "getPetName" component key, will I be able to access the underlying property?

  • <!--- Create an instance of the girl object. --->
  • <cfset girl = new Girl() />
  •  
  • <!---
  • Copy the getName() reference into the getPetName
  • key - remember, there was no implicit getter for
  • pet name.
  • --->
  • <cfset girl.getPetName = girl.getName />
  •  
  • <!--- Output the various values. --->
  • <cfoutput>
  •  
  • Name: #girl.getName()#<br />
  •  
  • <br />
  •  
  • Pet Name: #girl.getPetName()#
  •  
  • </cfoutput>

As you can see, all we are doing is copying the getName() reference to another key within the same component. If the accessors work off of their run-time names, then this would provide access to the petName property which was not given an implicit getter by default. When we run this code, however, we get the following page output:

Name: Tricia

Pet Name: Tricia

As you can see, copying the implicit getter to another run-time variable did not change its underlying execution. As such, we can be confident that the code behind ColdFusion 9's implicit (or rather synthesized) accessors is hard-coded and specific to the originating property.




Reader Comments

Jun 3, 2010 at 9:01 AM // reply »
1 Comments

Great stuff. I had a hard time with something similar in CF8. I was writing my own getters and setters, but had some problems figuring out a proper variable scope, because cfproperty had some issues with exactly this type of code.


Jun 3, 2010 at 10:03 AM // reply »
2 Comments

This is exactly what I anticipated would happen. I assume that the function pointer is overwritten and now they both point to the same method.

If this would have worked, it would have been a huge security gap in ColdFusion.


Jun 3, 2010 at 10:09 AM // reply »
10,640 Comments

@Steven,

It would have been a security gap; however, ColdFusion is so dynamic, that you can always plug in your own functions to existing CFCs at runtime to circumvent access rights.

So, what must be happening is that when the code is parsed, ColdFusion must actually read the CFProperty tags (which must be first in the file) and then compile individual getters / setter functions for each appropriate property.

I guess it makes sense; I just figured that this is the kind of thing that getFunctionCalledName() would be used for - otherwise, I have no idea for what core reason they added that method.


Jun 3, 2010 at 5:53 PM // reply »
6 Comments

Ben, you're a nut -- always digging deep into parts of CF that nobody in their right mind should ever use.

And that's why I *love* following your blog. Keep up the obsessively thorough work!


Jun 3, 2010 at 6:24 PM // reply »
10,640 Comments

@Ben,

Thanks my man. I just really like to know how this stuff works - to see how it can be leveraged for fun and profit (to borrow a phrase from Dan Wilson).


Gio
Oct 24, 2010 at 4:13 PM // reply »
1 Comments

Does anyone know if implicit getter and setter function are created as public or remote functions; can it be overridden?


Nov 7, 2010 at 8:02 PM // reply »
10,640 Comments

@Gio,

As far as I know, they are created as public; defaulting them to Remote would probably pose a security risk. Also, you can override them simply by defining your own. When ColdFusion builds the CFC, it will check to see if you have defined your own getter/setter methods before trying to synthesize them.


JF
Mar 28, 2011 at 1:30 PM // reply »
8 Comments

What I am wondering is why are they faster than the regular explicit function or at least they seem to be in the few test we've done.


Mar 28, 2011 at 2:07 PM // reply »
2 Comments

@JF, Since the classes they belong to are generated only once, I assume it will be directly compiled into a java class. And then it would make sense that it's faster in execution, because the interpreter for CFML is just skipped.

But then again, I'm just making an educated guess here. Maybe someone with more thorough knowledge of CF's inner workings can prove me wrong...


Mar 28, 2011 at 9:20 PM // reply »
10,640 Comments

@JF, @Steven,

Hmm, interesting question. I had assumed that a synthesized getter/setter actually got compiled down into the same code as any manually coded UDF; but, like @Steven, that's just a guess.

What would be an interesting experiment was to see if the method could be removed from the CFC instance with a structDelete( cfc, "getterName" ). If that works, I would assume that the method is being treated like any UDF, but I don't think I've tried this.... and to be fair, it really wouldn't prove anything one way or the other :D

At the end of the day, synthesized getters and setters are useful until they are not. I wouldn't worry too much about the actual performance.


Mar 28, 2011 at 9:20 PM // reply »
10,640 Comments

@JF, @Steven,

.... that said, Elliott Sprehn always preaches that you should *believe in the language*. That it will be able to do things more efficiently than anything you could do manually. So, in that mindset, it would make sense that the synthesized methods are *at least* as fast, if not faster than manually coded methods.


Mar 29, 2011 at 1:11 PM // reply »
1 Comments

Hi Ben,

Out of curiosity I tried deleting the implicit method with StructDelete (it worked). Tried to access it after the delete and it was still available and returned me the default value.

Did that as well :

<cfoutput>#obj.method#</cfoutput>

and here is the result

coldfusion.runtime.ImplicitGetter@47156c03

Seems like it is instantiated directly in the CF runtime (? I think)..


Mar 29, 2011 at 10:27 PM // reply »
10,640 Comments

@Fred,

Very interesting! Now my curiosity is piqued. I'll have to do some exploration. Thanks for doing some digging.


Mar 30, 2011 at 11:06 AM // reply »
10,640 Comments

@All,

I did some more in-depth exploration of the behavior of ColdFusion 9's implicit getters and setters.

http://www.bennadel.com/blog/2161-Exploring-ColdFusion-9-Implicit-Synthesized-Getter-And-Setter-Behavior.htm

They seem to be mostly like standard User Defined Functions. The only real difference I saw was that the internal property reference could not be overridden... which has me very curious.



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 8, 2012 at 11:09 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Here's a warning about using fixed top or bottom menu bars that hasn't been mentioned. Browsers don't factor fixed bars into the page height for page up and page down, meaning you'll have anything f ... read »
Feb 8, 2012 at 10:32 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
@Andy, Ah, very cool. FW/1 really seems to be quite well-rounded these days! ... read »
Feb 8, 2012 at 9:52 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
Just wanted to let you know that version 2.0 of Sean Corfield's FW/1 supports routing. This allows you to build true RESTful APIs using ColdFusion. (search for "URL Routes" https://github ... read »
Feb 8, 2012 at 2:05 AM
Creating A Fixed-Length Queue In JavaScript Using Arrays
Cool site ... read »
Feb 7, 2012 at 5:00 PM
Ask Ben: Ending ColdFusion Session When User Closes Browser
We've used code that sets the cookies without the "expires" attribute in most of our applications to accommodate an "automatic logoff" (Let's face it, that's what we're really try ... read »
Feb 7, 2012 at 10:10 AM
Ask Ben: Building An AJAX, jQuery, And ColdFusion Powered Application
Hey Ben great post. Just like @Carl Steinhilber I am having the same trouble with the -'parseerror'. But I can only reach GET contacts-demo.cfm I have added secureJSON="no" ... read »
ang
Feb 7, 2012 at 4:46 AM
Using The Apple iPod Shuffle Without iTunes
i got the same error with munkey!! help please!! ... read »
Feb 7, 2012 at 2:48 AM
Ask Ben: Javascript String Replace Method
@Kadut, . is a special character you will need to escape it \. ... read »