在Coldfusion cfc中,函数外部设置的变量的作用域名称是什么?
在Coldfusion组件/CFC中,我希望正确地确定一些变量的作用域,使其可用于所有包含的函数,但对外部脚本隐藏或阻止。cfc的内存作用域的名称是什么?是“变量”吗?这在包含的函数中可用吗?它是否被cfc外部阻挡 (参考文献8中的示例) 呼叫页面:在Coldfusion cfc中,函数外部设置的变量的作用域名称是什么?,coldfusion,cfc,Coldfusion,Cfc,在Coldfusion组件/CFC中,我希望正确地确定一些变量的作用域,使其可用于所有包含的函数,但对外部脚本隐藏或阻止。cfc的内存作用域的名称是什么?是“变量”吗?这在包含的函数中可用吗?它是否被cfc外部阻挡 (参考文献8中的示例) 呼叫页面: <cfset settings = structNew()> <cfset util = createObject("component", "myUtils").init(settings)> <cfoutput&g
<cfset settings = structNew()>
<cfset util = createObject("component", "myUtils").init(settings)>
<cfoutput>
#util.myFunction()#
</cfoutput>
<cfset c = createObject("component","myComponent").init()>
<cfoutput>
<!--- Should output "development" --->
#c.getMode()#
</cfoutput>
<cfset c = createObject("component","myComponent")>
<cfoutput>
#c.Mode#
</cfoutput>
#util.myFunction()#
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>
欢迎提出其他最佳做法建议。我已经很久没有这样做了。提前谢谢。我可能需要回答我自己的问题,以便下次在StackOverflow上需要时可以找到它。我不是很肯定,但我想这是怎么做到的。(一如既往,欢迎您提供更正和建议!)
呼叫页面:
<cfset settings = structNew()>
<cfset util = createObject("component", "myUtils").init(settings)>
<cfoutput>
#util.myFunction()#
</cfoutput>
<cfset c = createObject("component","myComponent").init()>
<cfoutput>
<!--- Should output "development" --->
#c.getMode()#
</cfoutput>
<cfset c = createObject("component","myComponent")>
<cfoutput>
#c.Mode#
</cfoutput>
#c、 getMode()#
是,它是默认的变量范围
<cfcomponent output="false">
<cfset variables.mode = "development">
<cffunction name="getMode" output="false">
<cfreturn variables.mode>
</cffunction>
</cfcomponent>
最好在CFC中的CFFunction中定义所有变量的范围
不要忘记output=“false”,它将减少CF生成的大量空白。我通常会省略access=“public”,因为这是默认设置
<> P>如果你希望别人浏览你的CFC,你甚至可以考虑使用< /P>
<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>
在ColdFusion组件中,所有公共名称都在
此
范围内,所有私有名称都在变量
范围内。名称可以包括“普通”变量属性和“UDF”方法。在ColdFusion组件中,This
和变量
作用域是每个实例的,不在实例之间共享
在ColdFusion组件外部,您可以使用结构表示法使用任何公共名称(组件中此范围内可用的名称)。您不能访问任何私人名称
默认范围始终是变量
——组件内、组件外、UDF内、组件方法内,等等
请注意,没有所谓的“内存”作用域。有命名作用域,但没有内存作用域。
<cfcomponent>
<cfset this.mode = "development">
</cfcomponent>
呼叫页面:
<cfset settings = structNew()>
<cfset util = createObject("component", "myUtils").init(settings)>
<cfoutput>
#util.myFunction()#
</cfoutput>
<cfset c = createObject("component","myComponent").init()>
<cfoutput>
<!--- Should output "development" --->
#c.getMode()#
</cfoutput>
<cfset c = createObject("component","myComponent")>
<cfoutput>
#c.Mode#
</cfoutput>
#c、 模式#
我会用cfargument逐个列出支持的设置,用hint=“”记录它们是什么,然后调用方可以使用init(ArgumentCollection=“#setting_struct”)如果有很多参数。这样更好的文档。@cf_PhillipSenn-我相信他们询问的是在cfc之外看不到的范围。