开发者

How do you dynamically return an implicitly set property in a CFC?

I'm got a CFC whose properties I want to return through a single function:

public string function getApplicationSetting(required string setting)
{
    return myCFC.getSetting()
}

The problem is that Setting needs to be dynamic. If it were a struct, I could do this:

r开发者_运维技巧eturn myCFC.variables[arguments.setting]

In other words, the Setting in getSetting() needs to reflect the incoming argument. Am I approaching this wrong? Is there a better way to do this?


Assuming you are in ColdFusion 8, you'll want to look at the onMissingMethod() function in ColdFusion.

Something like this untested example I just wrote up:

<cffunction name="onMissingMethod"> 
    <cfargument name="missingMethodName" type="string"> 
    <cfargument name="missingMethodArguments" type="struct"> 

    <cfif left(arguments.missingMethodName, 3) eq "get">
        <cfreturn variables[right(arguments.missingMethodName, len(arguments.missingMethodName)-3)] />
    </cfif>

</cffunction>

If you are in ColdFusion 9, then implicit getters are already a part of the deal if you define your properties correctly.

Component Person accessors=true {
    property firstname;
    property lastname;
    property age;
    property city;
    property state;
}

The above component will automatically have getLastname(), getFirstname(), etc.

Reference: http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜