2011-10-05 74 views
1

我得到了一個CFC我想通過一個函數返回其屬性:如何在CFC中動態返回隱式設置的屬性?

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

的問題是,Setting需求是動態的。如果它是一個結構,我可以這樣做:

return myCFC.variables[arguments.setting] 

換句話說,在SettinggetSetting()需求,以反映傳入的參數。我接近這個錯誤嗎?有一個更好的方法嗎?

+1

直視''或'評估()'用於與動態函數調用功能名稱 – Henry

回答

4

假設你在ColdFusion 8中,你需要查看ColdFusion中的onMissingMethod()函數。

像這樣的事情未經檢驗例如我剛寫了起來:

<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> 

如果你在ColdFusion的9,那麼隱含的干將已經是交易的一部分,如果你正確地定義你的屬性。

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

上述組件將自動擁有getLastname(),的getFirstName()等

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