If you create a lot of objects to store data in and are constantly creating get/set methods to handle the setting of those properties then, this will probably save you some time. Its a nice trick not to have to write all that code anymore (or even have to maintain that code if you use some sort of code generator).

Here is our component:


<!--- 
    Author: Steve Ross http://blog.stevensross.com
    Disclaimer: Free to use and distribute, please leave in the credit!
    --->
<cfcomponent output="false"  displayname="Simple on Missing Method Example" hint="Will let you do custom getters and setters w/o writing the code">

    <cffunction name="OnMissingMethod" returnType="any" access="public" output="true" hint="Handles getters and setters">
        <cfargument name="MissingMethodName" type="string" required="true" hint="The name of the missing method">
        <cfargument name="MissingMethodArguments" type="struct" required="true" hint="The arguments passed in to the missing method">

        <cfif LCase(arguments.MissingMethodName) CONTAINS "get">
            <cfreturn getValue(replace(arguments.MissingMethodName, "get", ""))/>
        <cfelseif LCase(arguments.MissingMethodName) CONTAINS "set">
            <cfif StructKeyExists(arguments.MissingMethodArguments, "2")>

            <cfelse>
                <cfset setValue(replace(arguments.MissingMethodName, "set", ""), arguments.MissingMethodArguments)/>
            </cfif>
        </cfif>
    </cffunction>

    <cffunction name="setValue" returnType="void" access="private" output="true" hint="internal setter method" >
        <cfargument name="name" type="string" required="true" hint="name of property to set">
        <cfargument name="value" type="any" required="true" hint="value of property">
            <cfset this[arguments.name] =  arguments.value.1 />
    </cffunction>

    <cffunction name="getValue" returnType="any" access="private" output="true" hint="internal getter method" >
        <cfargument name="name" type="string" required="true" hint="name of property to set">
            <cfif StructKeyExists(this, arguments.name)>
                <cfreturn this[arguments.name]>
            <cfelse>
                <cfreturn ""/>
            </cfif>
    </cffunction>

</cfcomponent>

I’m not really going to go into how this works, put simply all I am doing is a string replace to get the name and then I’m taking the argument and putting it as the data for that variable. Then when you call the getter I’m just striping out the word ‘get’ and finding your data. Now if you haven’t set the data we return empty string.

So save the above example and call it “Simple.cfc”

Create another CFM file in the same directory and call it “test.cfm”

Now you can see this work by putting this code in test.cfm:

<cfset testObj = CreateObject("Component", "Simple")/>
<cfset testStruct = {foo='bar'}/>

<cfset testObj.setTestStruct(testStruct)/>

<h4>We call the testObj.getTestStruct() Method</h4>
<cfdump var="#testObj.getTestStruct()#">

<h4>Notice the "testStruct" property</h4>
<cfdump var="#testObj#" label="dumping the entire object">

A more practical example:


<cfset User = CreateObject("Component", "Simple")/>

<cfset User.setFirstName('Steven')/>
<cfset User.setLastName('Ross')/>

<cfset tmpInfo = {password='dont do it', id='XXX'}/>

<cfset User.setInfo(tmpInfo)/>

<cfdump var="#User#" label="dumping the entire object">

Hope this helps someone out on what you can do with MissingMethod in ColdFusion 8

2 Responses to “Using onMissingMethod to create get/set methods”

  1. Alex Says:

    :)

  2. Alex Says:

    I found your site on technorati and read a few of your other posts. Keep up the good work. I just added your RSS feed to my Google News Reader. Looking forward to reading more from you down the road!

Sorry, comments are closed for this article.