Skip to main content
Known Participant
April 3, 2011
Question

Randomly getting entity has incorrect type for being called as a function

  • April 3, 2011
  • 3 replies
  • 5358 views

I have a good sized cfc with several functions in it, I am going through and trying to re-code it some so that it does not include cfobject's that refer to itself.

For example making a cfobject of the cfc as "Mainfunctions" and then calling functions such as cfset x=MainFunctions.MakeRandomString(Length=5)

I want to just do cfset x=MakeRandomString(Length=5)

Now the problem I am having is that I go through and make the rewrites and test it and it works fine, and then periodically I get a random error telling me

"Entity has incorrect type for being called as a function. The symbol you provided MyFunctionName is not the name of a function"

However testing it on the exact same page, with the exact same parameters it will work perfectly fine.

I am testing this on CF9, I have scoped out all the variables I can find, defining them at the start of the function using var.variablename and referencing the function arguments with arguments.ArgumentName

I am completely baffled as to why this is happening and appreciate any suggestions.

Thanks,

    This topic has been closed for replies.

    3 replies

    Known Participant
    April 6, 2011

    and there goes that theory... With the new update in place it still has the issue. I still have more code to clean so hopefully that will fix it when I upload the new functions file, but I'm still baffled on this.

    Inspiring
    April 4, 2011

    It sounds to me like you're not VARing your function-local variables, and you've got a variable name that is the same as one of your method names.

    If you don't VAR your variables (which you should always do, unless there's a specific reason to want something not VARed), then the variable will be created in the variables scope, which is where public methods are.  And because method names are just variables like any other variable, you can easily overwrite them.

    If you have this (let's see if the forums allow me to paste in code today):

    <cffunction name="getStuff" returntype="query">
        <cfquery name="getStuff">
            <!--- some SQL --->
        </cfquery>
    </cffunction>

    Then you will overwrite the function getStuff with the query named getStuff.  I see this sort of thing (once) all the time.  You need to VAR the query variable:

    <cffunction name="getStuff" returntype="query">
        <cfset VAR getStuff = "">
        <cfquery name="getStuff">
            <!--- some SQL --->
        </cfquery>
    </cffunction>

    Also... now I'm not saying this is exactly what you're doing, but it's seldom sensible to have a recordset name the same as the method name anyhow.  "getStuff" is not a meaningful name for a recordset... the recordset is the stuff, it's not the thing that gets it.  In this case it should probably best be called "stuff", eg:

    <cfset VAR stuff = "">

    <cfquery name="stuff">

    Poor naming policies like the above help create these situations, although the VARing (or lack-thereof) is the actual problem here.

    --

    Adam

    Known Participant
    April 4, 2011

    I had thought that as well when I saw the bug, I looked throughout the function itself and could not find the same name as the function I was calling being used as a variable of any sort. I was just thinking about it and even did a full search throughout the function file itself for "GetUserInformation" which is the function name. It only shows up as functions and never as a variable, query, or parameter.

    Since the function is within the same cfc, would it be a good idea to use this.GetUserInformation to call the function or would that possibly cause an issue down the road that I am missing, or is it just that I need to go through and convert every function to be fully scoped now (Which I will do, but I want to take these on one at a time)?

    Inspiring
    April 4, 2011

    Can you reliably replicate this?  It sounds perhaps like you can't.

    If you CAN, put a try/catch around the erroring call, and dump out the "function" to see what value it now holds.  That might give you a clue as to where it's being changed.

    The only time I specifically use the THIS scope in a CFC is to set public properties, which we do in some very specific situations.  I'd never use it to qualify a function call, no.  I don't see that it serves any useful purpose.

    I might have to recant my earlier statement... using an unVARed variable might overwrite a PRIVATE method, but it should not overwrite a PUBLIC one (which are in the this scope).  Not in a position to test this just now, as only have a 5min break... not enough time to knock some code together.  Will look @ it in more depth this evening, if I have a chance.

    --
    Adam

    Inspiring
    April 4, 2011

    Check where you are calling it from.  What you are describing could occur if the function is further down in the code from the line calling it.

    Known Participant
    April 4, 2011

    So if I understand you correctly, if I have a cfc kinda like this:

    cffunction name="x"

    code for function x

    end cffunction

    cffunction name="y"

    some code

    cfset abc=z(param)

    end cffunction

    cffcuntion name="z"

    some code

    end cffunction

    Then I must put function Y to be after Z to prevent the issue. If so then I don't think I can call any functions directly by name then reliably and must always create a cfobject within the cfc just to call the functions within it.