Search code examples
coldfusioncoldfusion-9coldfusion-10

CF8 vs. CF10: local scope declared in parent not visible in child functions


I have quite a bit of code written for ColdFusion 8 that is failing in ColdFusion 10. I realize that CF9 and later handles the "Local" scope differently than previous. My problem is that I've coded functions to extend each, in the sense that child instances inherit variables from the parent. But just to be clear exactly what I mean, let me give you an example of what was working and how it's failing in CF10.

Take two CFCs, a Child CFC that extends the Parent CFC:

<!--- Parent.cfc --->
<cfcomponent displayname="Parent">

    <cffunction name="SomeFunction" output="yes">
        <cfset local.parent_val = "Declared in parent instance">
    </cffunction>

</cfcomponent>



<!--- Child.cfc --->
<cfcomponent displayname="Child" extends="Parent">
    <cffunction name="SomeFunction" output="yes">
    <cfset local.child_val = "Declared in child instance">

    <!--- The following "super" call will instantiate the "parent_val" variable
    from within the parent CFC's version of this function... but only in CF8? --->
    <cfset Super.SomeFunction() />

    <p>#local.child_val#</p>
    <p>#local.parent_val#</p>
    </cffunction>
</cfcomponent>

Invoked as:

<cfset ChildCFC = CreateObject("component","Child") />
<cfoutput>#ChildCFC.SomeFunction()#</cfoutput>

In CF8 I get: Declared in child instance Declared in parent instance

In CF10 I get: Declared in child instance [error dump showing "Element PARENT_VAL is undefined in LOCAL."]

As you can see, in CF10, even after calling the parent version of the function (via "super") the local variable declared in the parent is not accessible in the child as it was under CF8.

So, my question is this: Is there any hope of recovering the architecture as designed in the examples above? If not, is there an easy migration trick that I'm missing?

A lot of my code expects a child-owned function to be able to "see" variables declared in the parent by use of the SUPER call (as in the example above). For those that faced this transition from CF8 to current, I'd appreciate your thoughts and suggestions here.


Solution

  • In your CF8 SomeFunction() in Parent.cfc

    local.parent_val = blah // sets to variables.local.parent_val, dies with object
    

    In your CF9 SomeFunction() in Parent.cfc

    local.parent_val = blah // eqv to var parent_val, dies right after function quits
    

    Your code in CF8 was wrong to begin with. If it was meant to be local to the function, it should have had a var local = {} on the very first line of the function. Then the behavior wouldn't have changed in CF9+.

    If your existing code base has not ran into thread-safety issue, then I think you can rename local. to variables. for variables that you expect to live on with the object and accessible later by the child.