在 Coldfusion cfc 中,函数外部设置的变量的作用域名称是什么?

发布于 2024-08-15 19:45:25 字数 1037 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

‖放下 2024-08-22 19:45:25

在 ColdFusion 组件中,所有公共名称都在 This 范围内,所有私有名称都在 Variables 范围内。名称可以包括“正常”变量属性以及“UDF”方法。在 ColdFusion 组件中,ThisVariables 作用域是每个实例的,并且不在实例之间共享。

在 ColdFusion 组件外部,您可以使用结构表示法来使用任何公共名称(在 This 范围内的组件内可用的名称)。您不得访问任何私人名称。

默认范围始终是变量 - 在组件内、组件外、UDF 内、组件方法内等。

请注意,不存在“内存”范围之类的东西。有命名范围,但没有内存范围。

Within a ColdFusion component, all public names are in the This scope and all private names are in the Variables scope. Names may include both "normal" variable properties as well as "UDF" methods. Within a ColdFusion component, the This and Variables 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.

傲鸠 2024-08-22 19:45:25

是的,这是默认的变量范围。

<cfcomponent output="false">
    <cfset variables.mode = "development">

    <cffunction name="getMode" output="false">
        <cfreturn variables.mode>
    </cffunction>
</cfcomponent>

最好将所有变量的范围限制在 CFC 的 cffunctions 中。

不要忘记 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>

Yes, it is the default, variables scope.

<cfcomponent output="false">
    <cfset variables.mode = "development">

    <cffunction name="getMode" output="false">
        <cfreturn variables.mode>
    </cffunction>
</cfcomponent>

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

<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>
你与昨日 2024-08-22 19:45:25

我可能必须回答我自己的问题,以便下次需要时可以在 StackOverflow 上找到它。我并不肯定,但我认为事情就是这样完成的。 (一如既往,欢迎更正和建议!)

<cfcomponent>
    <!--- Server Mode: production | development - used for differing paths and such --->
    <cfset variables.mode = "development">

    <cffunction name="init" access="public">
        <cfargument name="settings" type="struct" required="no">
        <cfset StructAppend(variables, arguments.settings)>
        <cfreturn this>
    </cffunction>

    <cffunction name="getMode" access="public">
        <cfreturn variables.mode>
    </cffunction>

</cfcomponent>

调用页面:

<cfset c = createObject("component","myComponent").init()>
<cfoutput>
    <!--- Should output "development" --->
    #c.getMode()#
</cfoutput>

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!)

<cfcomponent>
    <!--- Server Mode: production | development - used for differing paths and such --->
    <cfset variables.mode = "development">

    <cffunction name="init" access="public">
        <cfargument name="settings" type="struct" required="no">
        <cfset StructAppend(variables, arguments.settings)>
        <cfreturn this>
    </cffunction>

    <cffunction name="getMode" access="public">
        <cfreturn variables.mode>
    </cffunction>

</cfcomponent>

Calling page:

<cfset c = createObject("component","myComponent").init()>
<cfoutput>
    <!--- Should output "development" --->
    #c.getMode()#
</cfoutput>
心是晴朗的。 2024-08-22 19:45:25
<cfcomponent>
<cfset this.mode = "development">
</cfcomponent>

调用页面:

<cfset c = createObject("component","myComponent")>
<cfoutput>
#c.Mode#
</cfoutput>
<cfcomponent>
<cfset this.mode = "development">
</cfcomponent>

Calling page:

<cfset c = createObject("component","myComponent")>
<cfoutput>
#c.Mode#
</cfoutput>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文