2

I am using the technique detailed in this answer to manage a library of small utility functions. (Essentially, each function is loaded as a "mix-in" using cfinclude.)

I have need, however, to know the names of the functions that the object has (once instantiated). A cfdump on the object only shows the init function which is written directly in the CFC.

Some more detail:

I am creating the object in the application scope in OnApplicationStart().

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

However, to save the developers from having to constantly write application.udfs.foo(), I thought I'd grab all of the functions and drop them in to the variables scope in OnRequestStart(), so that these hypothetical developers could just write foo().

<cfset foo=application.udfs.foo>

Obviously, though, this needs to be dynamic and to happen for each of the functions in the object, no matter how many there are. If I repeat this line for every function I've lost whatever I'd gained by having a library that is dynamically generated.

I thought perhaps I could use a collection loop, but that was invalid. I am fairly certain there's a way to get the list of methods in an object, but I have not yet been able to find it.

Any clues?

By the by, my fallback is going to be to copy the application.udfs object to a local object with a nice short name (like "u") so that the developers can simply type u.foo(), so no need to suggest that if what I want to do can't be done.

Community
  • 1
  • 1
ale
  • 6,369
  • 7
  • 55
  • 65

3 Answers3

3

This should allow you to import all your udfs into the global variables scope:

StructAppend(variables, application.udfs);
Jake Feasel
  • 16,785
  • 5
  • 53
  • 66
1

I think GetMetaData should help you.

Sergey Galashyn
  • 6,946
  • 2
  • 19
  • 39
  • Ah, thanks for the reminder! Unfortunately, the array of functions it returns is just the `init`, like I saw with `cfdump`. – ale Dec 08 '11 at 17:56
  • @AlEverett Oops! This is because I'm using Railo. It is smart enough to expose included chunks of CFC, both for `cfdump` and `GetMetaData`. – Sergey Galashyn Dec 08 '11 at 18:48
1

Here another interesting option suggested by Ben Nadel:

Check out the detail in his blog entry: 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" )
    ) />

Micah
  • 1,221
  • 17
  • 42