获取注入CFC的函数名称?

时间:2011-12-08 17:04:16

标签: coldfusion cfc

我正在使用this answer中详述的技术来管理小型实用程序函数库。 (基本上,每个函数都使用cfinclude作为“混合”加载。)

但是,我需要知道对象具有的函数的名称(一旦实例化)。对象上的cfdump仅显示直接在CFC中编写的init函数。

更多细节:

我正在OnApplicationStart()中的应用程序范围内创建对象。

<cfset application.udfs=createObject("component","extensions.udfs").init()>

但是,为了避免开发人员不得不经常编写application.udfs.foo(),我认为我会抓住所有函数并将它们放入OnRequestStart()中的变量范围,以便这些假设的开发人员可以写foo()

<cfset foo=application.udfs.foo>

显然,这需要是动态的,并且对于对象中的每个函数都要发生,无论有多少。如果我为每个函数重复这一行,那么我已经失去了通过动态生成的库获得的任何东西。

我想也许我可以使用收集循环,但那是无效的。我相当确定有一种方法可以获取对象中的方法列表,但我还没有找到它。

任何线索?

通过by,我的后备将是将application.udfs对象复制到一个具有漂亮短名称的本地对象(如“u”),以便开发人员可以只键入u.foo(),所以没有需要建议如果我想做的事情无法完成。

3 个答案:

答案 0 :(得分:3)

这应该允许您将所有udfs导入全局变量范围:

StructAppend(variables, application.udfs);

答案 1 :(得分:1)

我认为GetMetaData可以帮到你。

答案 2 :(得分:1)

这是Ben Nadel建议的另一个有趣选项:

查看博客条目中的详细信息:http://www.bennadel.com/blog/1776-Creating-Globally-Accessible-User-Defined-Functions-In-ColdFusion-Safer-Version-.htm

UDF.cfc

<cfcomponent
    output="false"
    hint="I define user defined functions.">

    <cffunction
        name="getMessage"
        access="public"
        returntype="string"
        output="false"
        hint="I return a test message.">

        <cfreturn "I am defined in the UDF component" />
    </cffunction>

</cfcomponent>

的Application.cfc

<!--- Define the application. --->
<cfset this.name = hash( getCurrentTemplatePath() ) />
<cfset this.applicationTimeout = createTimeSpan( 0, 0, 5, 0 ) />


<!---
    Add all of our "global" methods to the URL scope. Since
    ColdFusion will automatically seach the URL scope for
    non-scoped variables, it will find our non-scoped method
    names.
--->
<cfset structAppend(
    url,
    createObject( "component", "UDF" )
    ) />