Mozier's weblog

<cfweblog author="Mozier" attention="ColdFusion,BlueDragon,CFML,Model-Glue,Mach-ii,etc" />
  订阅 订阅  :: 管理

CFC中Methods的访问权限

Posted on 2004-08-16 17:24  Mozier  阅读(741)  评论(0编辑  收藏  举报

cffunction标签中access属性有4个:private package public和 remote (私有、包裹、公有、远程)。

private方法只能给CFC中的其他函数或子类(CFC)访问

package只能给同一目录下的CFC中的函数访问

public方法可以给服务器上的CFC、CFM页面访问

而 remote 不仅可以给服务器上的CFC、CFM页面访问,而且可以URL调用,FORM表单调用,FLASH调用,并提供web服务

由次可见remote的访问权限最高,public次之,private最低。

举个最简单的例子:

<!--- sth.cfc --->
<cfcomponent>
  <cffunction name="privateMethods" access="private" returntype="string">
    <cfreturn "bala bala bala">
  </cffunction>
  <cffunction name="publicMethods" access="public" returntype="any">
  <cfreturn privateMethods()>
  </cffunction>
</cfcomponent>

<!--- test.cfm --->
<cfscript>
obj=CreateObject("component","sth");
temp=obj.publicMethods();
writeOutput (temp);
</cfscript>

当test.cfm访问sth.cfc中的私有方法“privateMethods”,页面提示找不到sth.cfc中的privateMethods方法,无法访问,但访问sth.cfc中的公有方法publicMethods,页面输出“bala bala bala”,访问正常,同时证明了私有函数privateMethods只能被sth.cfc中的publicMethods函数访问。