Phi*_*las 4 coldfusion coldfusion-9 coldfusion-10
我为ColdFusion 8编写了相当多的代码,在ColdFusion 10中失败.我意识到CF9和更高版本处理"Local"范围的方式与之前不同.我的问题是我编写了扩展每个函数的函数,因为子实例从父类继承变量.但是为了清楚地说明我的意思,让我举一个例子说明CF10的工作原理以及它是如何失败的.
采取两种CFC,一种扩展父CFC的儿童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>
Run Code Online (Sandbox Code Playgroud)
调用为:
<cfset ChildCFC = CreateObject("component","Child") />
<cfoutput>#ChildCFC.SomeFunction()#</cfoutput>
Run Code Online (Sandbox Code Playgroud)
在CF8中,我得到:
Declared in child instance
Declared in parent instance
在CF10中,我得到:
Declared in child instance
[error dump showing "Element PARENT_VAL is undefined in LOCAL."]
正如您所看到的,在CF10中,即使在调用函数的父版本(通过"super")之后,父级中声明的局部变量也不能在子级中访问,因为它在CF8下.
所以,我的问题是:是否有希望恢复上述示例中设计的架构?如果没有,是否有一个我丢失的简单迁移技巧?
我的很多代码都希望子拥有的函数能够通过使用SUPER调用"看到"在父代中声明的变量(如上例所示).对于那些面临从CF8到现在的过渡的人,我在此感谢您的想法和建议.
在Parent.cfc中的CF8 SomeFunction()中
local.parent_val = blah // sets to variables.local.parent_val, dies with object
Run Code Online (Sandbox Code Playgroud)
在Parent.cfc中的CF9 SomeFunction()中
local.parent_val = blah // eqv to var parent_val, dies right after function quits
Run Code Online (Sandbox Code Playgroud)
您在CF8中的代码开头是错误的.如果它是函数的本地,它应该var local = {}在函数的第一行有一个.然后CF9 +中的行为不会改变.
如果现有的代码库还没有跑进线程安全的问题,那么我认为你可以重命名local.,以variables.对您希望与对象和可后来孩子住在变数.
| 归档时间: |
|
| 查看次数: |
410 次 |
| 最近记录: |