在 Coldfusion cfc 中,函数外部设置的变量的作用域名称是什么?
在 Coldfusion 组件/CFC 中,我希望正确确定一些变量的范围,使其可用于所有包含的函数,但对外部脚本隐藏或阻止。 cfc 的内存范围的名称是什么?是“变量”吗?它在包含的函数中可用吗?是不是被cfc外部屏蔽了?
(CF 8 中的示例)
调用页面:
<cfset settings = structNew()>
<cfset util = createObject("component", "myUtils").init(settings)>
<cfoutput>
#util.myFunction()#
</cfoutput>
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>
欢迎提供其他最佳实践建议。我已经有一段时间没有做这件事了。提前致谢。
In a Coldfusion component / CFC, I want to properly scope some variables to be available for all contained functions, but to be hidden or blocked from outside scripts. What is the name of the cfc's memory scope? Is it 'variables'? Is that available inside a contained function? Is it blocked from outside the cfc?
(Examples in CF 8)
Calling page:
<cfset settings = structNew()>
<cfset util = createObject("component", "myUtils").init(settings)>
<cfoutput>
#util.myFunction()#
</cfoutput>
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>
Additional best practice suggestions are welcomed. It's been quite a while since I've done this. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 ColdFusion 组件中,所有公共名称都在
This
范围内,所有私有名称都在Variables
范围内。名称可以包括“正常”变量属性以及“UDF”方法。在 ColdFusion 组件中,This
和Variables
作用域是每个实例的,并且不在实例之间共享。在 ColdFusion 组件外部,您可以使用结构表示法来使用任何公共名称(在
This
范围内的组件内可用的名称)。您不得访问任何私人名称。默认范围始终是变量 - 在组件内、组件外、UDF 内、组件方法内等。
请注意,不存在“内存”范围之类的东西。有命名范围,但没有内存范围。
Within a ColdFusion component, all public names are in the
This
scope and all private names are in theVariables
scope. Names may include both "normal" variable properties as well as "UDF" methods. Within a ColdFusion component, theThis
andVariables
scopes are per-instance and are not shared between instances.Outside a ColdFusion component, you may use any public names (names that would be available within the component in the
This
scope) using the struct notation. You may not access any private names.The default scope is always
Variables
- within a component, outside of a component, within a UDF, within a component method, etc.Note that there is no such thing as a "memory" scope. There are named scopes, but not memory scopes.
是的,这是默认的变量范围。
最好将所有变量的范围限制在 CFC 的 cffunctions 中。
不要忘记 output="false",它会减少 CF 生成的大量空白。我通常会省略 access="public" 因为这是默认值。
如果您希望在其他人浏览您的 CFC 时获得更好的文档,您甚至可以考虑使用
Yes, it is the default, variables scope.
It is a good idea to scope all your variables in cffunctions in CFC.
Don't forget output="false", it will cut down a lot of whitespace CF generates. I usually will leave out access="public" since that's the default.
If you want better documentation when others browse to your CFC, you may even consider using
我可能必须回答我自己的问题,以便下次需要时可以在 StackOverflow 上找到它。我并不肯定,但我认为事情就是这样完成的。 (一如既往,欢迎更正和建议!)
调用页面:
I may have to answer my own question so that I can find it next time I need it here on StackOverflow. I'm not positive, but I think this is how it's done. (As always, corrections and suggestions are welcome!)
Calling page:
调用页面:
Calling page: