在Coldfusion组件/ CFC中,我想正确地将一些变量的范围限定为可用于所有包含的函数,但是要隐藏或阻止外部脚本.cfc的内存范围是什么名字?这是'变量'吗?是否在包含的函数内可用?它是否被阻止在cfc之外?
(CF 8中的例子)
致电页面:
<cfset settings = structNew()>
<cfset util = createObject("component", "myUtils").init(settings)>
<cfoutput>
#util.myFunction()#
</cfoutput>
Run Code Online (Sandbox Code Playgroud)
myUtils.cfc:
<cfcomponent>
<!--- Need to set some cfc global vars here --->
<cffunction name="init" access="public">
<cfargument name="settings" type="struct" required="no">
<!--- I need to merge arguments.settings to the cfc global vars here --->
<cfreturn this>
</cffunction>
<cffunction name="myFunction" access="public">
<cfset var result = "">
<!--- I need to access the cfc global vars here for init settings --->
<cfreturn result>
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
欢迎提供其他最佳实践建议.我做完这件事已经有一段时间了.提前致谢.
yfe*_*lum 14
在ColdFusion组件中,所有公共名称都在This范围内,所有私有名称都在Variables范围内.名称可以包括"正常"变量属性以及"UDF"方法.在ColdFusion组件中,This和Variables范围是按实例进行的,不在实例之间共享.
在ColdFusion组件之外,您可以使用This结构表示法使用任何公共名称(在作用域中的组件中可用的名称).您可能无法访问任何私人名称.
默认范围始终是Variables- 在组件内,组件外部,UDF内,组件方法中等.
请注意,没有"内存"范围.有命名范围,但没有内存范围.
是的,它是默认的变量范围.
<cfcomponent output="false">
<cfset variables.mode = "development">
<cffunction name="getMode" output="false">
<cfreturn variables.mode>
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
最好将所有变量的范围放在CFC的cffunction中.
不要忘记output ="false",它会减少很多CF生成的空白.我通常会省略access ="public",因为这是默认值.
如果您想在其他人浏览到您的CFC时获得更好的文档,您甚至可以考虑使用
<cfcomponent output="false">
<cfproperty name="mode" type="string" default="development" hint="doc...">
<cfset variables.mode = "development">
<cffunction name="getMode" output="false">
<cfreturn variables.mode>
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)