Warning about onMissingMethod

Tonight I played around with onMissingMethod, a cool new feature in ColdFusion 8. See Ben Nadel’s entry on it for more detail. I knew that the method took two arguments - the name of the method invoked and a struct of the arguments that was passed.

What I didn’t realize and was surprised by - you can’t rename these arguments. So consider this:

<cffunction name="onMissingMethod" access="public" returnType="any" output="false"> <cfargument name="method" type="string" required="true"> <cfargument name="args" type="struct" required="true">

While the method itself will run, the arguments above will not actually be used. You must name them as Ben describe’s in his entry:

<cffunction name="onMissingMethod" access="public" returnType="any" output="false"> <cfargument name="missingMethodName" type="string" required="true"> <cfargument name="missingMethodArguments" type="struct" required="true">

As a side note - for the life of me I couldn’t find any mention of onMissingMethod in the docs - neither the Developer’s Guide nor the Reference. Can anyone else find it?

This is the code I’m using in a generic bean:

<cfcomponent name="Core Bean" output="false">

<cffunction name=”onMissingMethod” access=”public” returnType=”any” output=”false”> <cfargument name=”missingMethodName” type=”string” required=”true”> <cfargument name=”missingMethodArguments” type=”struct” required=”true”> <cfset var key = ““>

&lt;cfif find("get", arguments.missingMethodName) is 1&gt;
	&lt;cfset key = replaceNoCase(arguments.missingMethodName,"get","")&gt;
	&lt;cfif structKeyExists(variables, key)&gt;
		&lt;cfreturn variables[key]&gt;

&lt;cfif find("set", arguments.missingMethodName) is 1&gt;
	&lt;cfset key = replaceNoCase(arguments.missingMethodName,"set","")&gt;
	&lt;cfif structKeyExists(arguments.missingMethodArguments, key)&gt;
		&lt;cfset variables[key] = arguments.missingMethodArguments[key]&gt;


</cfcomponent> </code>

This let’s me define a simple bean like so:

<cfcomponent output="false" extends="bean">

<cfset variables.id = ““> <cfset variables.username = ““> <cfset variables.password = ““> <cfset variables.name = ““> <cfset variables.email = ““>

</cfcomponent> </code>

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com