Skip to main content
Participant
February 8, 2012
Answered

Cffunction scope in application.cfm?

  • February 8, 2012
  • 2 replies
  • 2236 views

Sorry, new to CF. 

I created a cffunction in application.cfm, but I find I can't call it from other cfm files.  I have application variables that I created and I can reference those, but trying to call the function gives me a "not found" error.  I'm sure this is a scoping problem that I don't understand.  How should I declare or reference the function to make it globally available?

    This topic has been closed for replies.
    Correct answer BKBK

    pbm233 wrote:

    I created a cffunction in application.cfm, but I find I can't call it from other cfm files.  I have application variables that I created and I can reference those, but trying to call the function gives me a "not found" error.  I'm sure this is a scoping problem that I don't understand.  How should I declare or reference the function to make it globally available?

    No special declarations or references are needed. Calling the function on any page within the scope of the application file should work.

    To verify this yourself, do the following test. Create a directory and place the files Application.cfm and testPage.cfm in it.

    Application.cfm

    <cfapplication name="cftest"

        sessionmanagement="Yes"

        setclientcookies="Yes"

        sessiontimeout="#createTimeSpan(0,0,60,0)#"

        applicationtimeout="#createTimeSpan(1,0,0,0)#">

       

        <cffunction name="f" returntype="string" >

            <cfreturn "This string is the result of a function call in Application.cfm">

        </cffunction>

    testPage.cfm

    <cfoutput>#f()#</cfoutput>

    Now, run the test page. I expect the string to be output. My guess is that it failed in your case because you named your file application.cfm instead of Application.cfm.

    2 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    February 9, 2012

    pbm233 wrote:

    I created a cffunction in application.cfm, but I find I can't call it from other cfm files.  I have application variables that I created and I can reference those, but trying to call the function gives me a "not found" error.  I'm sure this is a scoping problem that I don't understand.  How should I declare or reference the function to make it globally available?

    No special declarations or references are needed. Calling the function on any page within the scope of the application file should work.

    To verify this yourself, do the following test. Create a directory and place the files Application.cfm and testPage.cfm in it.

    Application.cfm

    <cfapplication name="cftest"

        sessionmanagement="Yes"

        setclientcookies="Yes"

        sessiontimeout="#createTimeSpan(0,0,60,0)#"

        applicationtimeout="#createTimeSpan(1,0,0,0)#">

       

        <cffunction name="f" returntype="string" >

            <cfreturn "This string is the result of a function call in Application.cfm">

        </cffunction>

    testPage.cfm

    <cfoutput>#f()#</cfoutput>

    Now, run the test page. I expect the string to be output. My guess is that it failed in your case because you named your file application.cfm instead of Application.cfm.

    pbm233Author
    Participant
    February 9, 2012

    That's it exactly!  All of these projects have file "application.cfm" instead of "Application.cfm".   I don't understand is why application variables could be referenced but not functions, but that is for another day...

    Owainnorth
    Inspiring
    February 9, 2012

    If you're new to CF, you should not be using Application.cfm - use Application.cfc. The former is now depricated, the latter will allow you to do what you're after quite easily.

    What I tend to do is wrap up all your similar functions into a CFC, then add that into the application scope during onApplicationStart. Create a CFC for your functions:

    [myfunctions.cfc]

    <cfcomponent>

      <cffunction name="doSomething" access="public" returntype="string">

        <cfreturn "hello world" />

      </cffunction>

    </cfcomponent>

    [Application.cfc]

    <cffunction name="onApplicationStart">

      <cfset application.MyFunctions = createObject("component", "myfunctions") />

    </cffunction>

    [index.cfm]

    <cfoutput>#application.MyFunctions.doSomething()#</cfoutput>

    Should do exactly what you're after. But ditch the Application.cfm.

    Inspiring
    February 9, 2012

    Should do exactly what you're after. But ditch the Application.cfm.

    Seconded.

    I'd also recommend reading all this lot:

    "Building and Using ColdFusion Components"

    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fec.html

    "Defining the application and its event handlers in Application.cfc"

    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7d39.html

    "Migrating from Application.cfm to Application.cfc"

    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0b63c-7ffa.html

    --

    Adam

    pbm233Author
    Participant
    February 9, 2012

    Thanks very much, got it.  I inherited a number of CF apps that use application.cfm, but I see now how I can start the migration.