我注意到,当我调用以前包含的(cfinclude).cfm文件的函数时,此时发生的所有冷聚变导入(cfimport)都被遗忘了。就像你没有导入任何东西一样。我发现了这种非常奇怪的行为,所以我把它隔离了出来,但结果保持不变,即使在冷融合10中也是如此。
我的设置:
/例
- 函数.cfm
- 索引.cfm
- /组件
- MyCFC.cfc
功能.cfm:
<cfscript>
function test(){
return "test";
}
</cfscript>
components/MyCFC.cfc:
component output="false"{}
index.cfm:
<cfscript>
include "functions.cfm";
import components.MyCFC;
foo = test();
bar = new MyCFC();
</cfscript>
This code will throw a coldfusion error : "Could not find the ColdFusion component or interface MyCFC".
when foo = test();
is removed or placed after bar = new MyCFC();
, the code runs just fine.
It doesn't matter if the import is placed before or after the include. Whenever an included function is called, the imports are forgotten.
Is this this a bug or is it supposed to behave this way?
Tested in coldfusion 9,0,0,251028 and coldfusion 10,282462
Since I do a lot of imports, I recently met some odd behavior and reported it do the CF9-bugbase: https://bugbase.adobe.com/index.cfm?event=bug&id=3288035
ColdFusion resolves the imports only for the current file and whenever you call a different file, it's like the "execution context" switches to that files with it's imports. So in your case when you execute new ColdFusion looks in functions.cfm for the imports. When you then call a method of the current file, it switches back and finds the import.
If I am right, your code should work, if you execute bar = new MyCFC();
directly after the import. Or you could define another method in the index.cfm and call it, before you create the class.
To work around that bug you need to make sure, that the import is resolved (on first use in the objects lifecycle) before that "context switching" takes place. So call new MyCFC();
before the external method.
@Adobe: Would be nice to fix this;)
I'm with @PeterBoughton, this sounds like a bug. I couldn't find a similar bug being reported either so I would go ahead with submitting it. In the mean time if you scrap "import", you should still be able to reference the CFC using:
bar = new components.MyCFC();
大收获!