The Dopefly Tech Blog

« The Dopefly Tech Blog Main page

Fun with Functions and CFCs III - Method Injection

posted under category: ColdFusion on December 12, 2007 by Nathan

Using the code from the previous entry in this series, Fun with Functions and CFCs II, we can take the example further and inject a new method into a CFC:

<cfscript>
  function methodToInject() {return "This method was Injected";}
  myCFC.getVariables ().injectedMethod = methodToInject; // inject a method into CFC "variables" scope
  myCFC.getVariables().this.injectedMethod = methodToInject; // also into public "this" scope
  writeOutput( myCFC.getVariables ().privateMethod() ); // call private method from test.cfc
  writeOutput( myCFC.injectedMethod() ); // call injected method
</cfscript>


We injected the method into both variables and this scopes so it would be available internally and externally.

Even more useful, you can replace existing functions:

<cfscript>
  function h4x0r3d() {return "This method has been hacked!"} // create a 'hacked' message function
  myCFC.getVariables().privateMethod = h4x0r3d; // replace privateMethod() with this function
</cfscript>

The next time anyone makes a call to privateMethod(), they will get "This method has been hacked!"

And deleting:
<cfscript>
  structDelete(myCFC.getVariables(), "publicMethod"); // delete publicMethod from internal variables scope
  structDelete(myCFC, "publicMethod"); // delete publicMethod from internal variables scope
</cfscript>

We had to delete it out of the public and private scopes (this and variables, respectively) - each scope had a pointer to the publicMethod() function.

Now, with a persistent component, like an application-scoped object, this can have long-lasting effects, but there could be many good uses for it, for example with an application lifecycle altering event such as a database crash, you could switch out calls to the database to instead return empty queries or a message.

Truthfully, it would probably be smarter to replace the entire component such as with a strategy pattern.

Nathan is a software developer at The Boeing Company in Charleston, SC. He is essentially a big programming nerd. Really, you could say that makes him a nerd among nerds. Aside from making software for the web, he plays with tech toys and likes to think about programming's big picture while speaking at conferences and generally impressing people with massive nerdiness and straight-faced sarcastic humor. Nathan got his programming start writing batch files in DOS. It should go without saying, but these thought and opinions have nothing to do with Boeing in any way.
This blog is also available as an RSS 2.0 feed. Click your heels together and click here to contact Nathan.