【问题标题】:put argument's struct in variables scope将参数的结构放在变量范围内
【发布时间】:2013-09-12 10:16:08
【问题描述】:

我想轻松地将参数的结构内容放入组件所有函数的变量范围内。 'Title' 是 searchitems 结构之一。

<cffunction name="setSearch" acces="public" returntype="void">    
     <cfargument name="searchitems" type="struct" required="true" />
     <cfset variables = arguments.searchitems>
     <cfset variables.test = "yo">           
</cffunction>

<cffunction name="testit" acces="public" returntype="void">
    <cfdump var="#variables.test#"><br>
    <cfif isdefined('variables.test')>found in variables.test  </cfif>
    <cfif isdefined('variables.variables.test')>found in variables.variables.test </cfif>
    <hr>
    <cfdump var="#variables.title#"><br>
    <cfif structkeyexists(variables,'title')>found in variables.title with structkeyexists </cfif>
    <cfif structkeyexists(variables.variables,'title')>found in variables.variables.title with structkeyexists</cfif>
    <cfif isdefined('variables.title')>found in variables.title </cfif>
    <cfif isdefined('variables.variables.title')>found in variables.variables.title</cfif>
</cffunction>

但是运行它会给出:

yo
found in variables.test

mytitletext
found in variables.variables.title with structkeyexists
found in variables.variables.title 

我觉得很奇怪,标题可以转储或作为 variables.title 输出,但不能用 isDefined 或 structkeyexists 检测到。有没有更有效的分配方式

<cfset variables = arguments.searchitems> 

【问题讨论】:

  • 首先,您需要了解任何对象的私有方法都驻留在变量范围内,并且按照您的方式进行操作将从 CFC 实例中清除它们。您可能想稍微抽象一下,例如:variables.searchItems = arguments.searchItems,这样您就不会随意删除变量范围内的其他内容。稍后将查看您的其余逻辑...
  • 最简单的方法是使用属性。有些人可能会认为这是编程环境中的一种时尚失礼,但它仍然是最简单的方法。
  • 当您设置variables = arguments.searchitems 时,您实际上是在变量范围内创建了一个名为“variables”的new 结构,即variables.variables。换句话说,您的variables 结构不会取代真正的variables 范围。当你转储variables.title 时,CF 实际上是给你variables.variables.title。正在推断范围。 StructKeyExists( variables,"title" ) 失败,因为变量 scope 中没有 title 变量(尽管它存在于您的 variables 结构中)。这有意义吗?

标签: coldfusion scope arguments cfc cffunction


【解决方案1】:

使用组件的“this”范围。

<cfcomponent>

<cfset this.myArgs = StructNew()>
<cfset this.test = "">


<cffunction name="setSearch" acces="public" returntype="void">    
     <cfargument name="searchitems" type="struct" required="true" />
     <cfset this.myArgs= arguments>
     <cfset this.test = "yo">           
</cffunction>

</cfcomponent>

【讨论】:

【解决方案2】:

我建议您遵循 Adam 的建议,并将您的 searchitems 保留在变量范围内自己单独的结构中,而不是作为单独的项目。这样您就不会冒险覆盖其他变量。

Test.cfc

<cfcomponent>

    <cffunction name="init">
        <!--- Set up a separate empty container for the searchitems to be available to all functions --->
        <cfset variables.searchitems = StructNew()>
        <cfreturn this>
    </cffunction>

    <cffunction name="setSearch" returntype="void">    
        <cfargument name="searchitems" type="struct" required="true">
        <!--- Fill the container with the struct passed into this function --->
        <cfset variables.searchitems = arguments.searchitems>
    </cffunction>

    <cffunction name="dumpSearchTitle" returntype="void">
        <cfdump var="#variables.searchitems.title#">
    </cffunction>

</cfcomponent>

index.cfm

<cfscript>
    request.searchitems = StructNew();
    request.searchitems.title = "mytitletext";
    test = CreateObject( "component", "test" );
    test.setSearch( request.searchitems );
    test.dumpSearchTitle();
</cfscript>

但是,如果在变量范围内拥有个人 searchitems 真的很重要,那么您可以将它们附加到变量范围内。 StructAppend 的第三个 false 参数确保您不会覆盖现有变量。

Test.cfc

<cfcomponent>

    <cffunction name="init">
        <cfreturn this>
    </cffunction>

    <cffunction name="setSearch" returntype="void">    
        <cfargument name="searchitems" type="struct" required="true">
        <cfset StructAppend( variables,arguments.searchitems,false )>
    </cffunction>

    <cffunction name="dumpSearchTitle" returntype="void">
        <cfdump var="#variables.title#">
    </cffunction>

</cfcomponent>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多