Skip to main content
WolfShade
Legend
May 20, 2015
Answered

One CFFUNCTION calling another in same CFC - using "this" isn't working

  • May 20, 2015
  • 1 reply
  • 1020 views

Hello, all,

I've got a CFC (let's call it test.cfc) that is supposed to handle a lot of  internal calls to other CFFUNCTIONs.

I had read that using "this.functionName(args);" should work.

However, for some reason it isn't.  I still have to use "components.compName().functionName(args);" for it to work.  Pseudo-code:

<cffunction name="functionA" access="private" output="no" returntype="any">

    <cfquery name="getThese" datasource="#application.thisDSN#">

      SELECT * FROM tableA ORDER BY ID

    </cfquery>

    <cfreturn getThese />

</cffunction>

and

<cffunction name="functionB" access="private" output="yes" returntype="void">

    <cfset tmp = this.functionA() /><cfoutput>#tmp#</cfoutput>

</cffunction>

This produces the error: Ensure that the name is correct and that the component or interface exists.

But the following works just fine:

<cffunction name="functionB" access="private" output="yes" returntype="void">

    <cfset tmp = components.test().functionA() /><cfoutput>#tmp#</cfoutput>

</cffunction>

Any suggestions as to why "new this.test().functionA();" doesn't work?

V/r,

^_^

    This topic has been closed for replies.
    Correct answer haxtbh

    The this scope is a public scope. You will need to change your functions to be access="public"


    Note: The This scope identifier works like the This keyword in JavaScript and ActionScript. CFCs do not follow the Java class model, and the This keyword behaves differently in ColdFusion than in Java. In Java, This is a private scope, whereas in ColdFusion, it is a public scope.

    CFC variables and scope - ColdFusion, English documentation - Adobe Learning Resources

    1 reply

    haxtbhCorrect answer
    Inspiring
    May 21, 2015

    The this scope is a public scope. You will need to change your functions to be access="public"


    Note: The This scope identifier works like the This keyword in JavaScript and ActionScript. CFCs do not follow the Java class model, and the This keyword behaves differently in ColdFusion than in Java. In Java, This is a private scope, whereas in ColdFusion, it is a public scope.

    CFC variables and scope - ColdFusion, English documentation - Adobe Learning Resources

    WolfShade
    WolfShadeAuthor
    Legend
    May 21, 2015

    AH!  Thanks, haxtbh‌!  I didn't see that part (I am in a bit of a rush.)

    V/r,

    ^_^