Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe ColdFusion

  • Print
  • + Share This
This chapter is from the book

Using the OnMissingMethod Method

It would be nice if all the CFCs that we write could handle their own errors. Starting in ColdFusion 8, any component can have a special method named OnMissingMethod that will run whenever code attempts to run a method that hasn't been defined in it. We can use this method to serve several purposes:

  • Implement custom error handling. The OnMissingMethod method can be especially useful when methods in different child classes might be called, even though they are not implemented in a particular component. For example, we could use OnMissingMethod to handle a call to getActorSalary made to a director component.
  • Run different code for several methods in a common place. If the same code can take the place of several methods, OnMissingMethod can provide a way to run the common code from a single point. This approach is not recommended, however; it's more straightforward to define all the methods separately and call the common code from each.
  • Act as a proxy that calls another object or component that will actually implement the function. For example, you could create a component that is empty except for onMissingMethod that takes any method passed to it and calls a Web service on another machine that consumes the method name and its arguments and returns a value. This approach is a good way to implement a flexible, distributed system.

The onMissingMethod function takes exactly two arguments, which contain the name of the method that was being called and a structure with the arguments that were passed to it. For example, here is a simple onMissingMethod method:

<cffunction name="onMissingMethod">
  <cfargument name="missingMethodName" type="string">
  <cfargument name="missingMethodNameArguments" type="struct">
    Hey! You called <cfoutput>#arguments.missingMethodName#</cfoutput> and I haven't
got one!
  • + Share This
  • 🔖 Save To Your Account