在不使用Evaluate()的情况下动态调用属于实例化cfc的函数,可以通过以下方法实现:
在ColdFusion中,可以使用getComponentMetaData()
函数获取CFC组件的元数据,然后通过反射来调用相应的函数。
<cfset myComponent = createObject("component", "path.to.myComponent")>
<cfset metaData = getComponentMetaData(myComponent)>
<cfset functionName = "myFunction">
<cfif structKeyExists(metaData, functionName)>
<cfset result = invoke(myComponent, functionName, arguments)>
<cfelse>
<cfthrow message="Function #functionName# not found in component.">
</cfif>
onMissingMethod()
:在CFC组件中,可以定义一个onMissingMethod()
函数来捕获未定义的方法调用。当尝试调用不存在的方法时,ColdFusion会自动调用onMissingMethod()
函数。
<cffunction name="onMissingMethod" access="public" output="false" returntype="any">
<cfargument name="missingMethodName" type="string" required="true">
<cfargument name="missingMethodArguments" type="struct" required="true">
<cfset var result = "">
<cfif isDefined("variables.instance.#missingMethodName#")>
<cfset result = evaluate("variables.instance.#missingMethodName#(argumentCollection=missingMethodArguments)")>
<cfelse>
<cfthrow message="Method #missingMethodName# not found in component.">
</cfif>
<cfreturn result>
</cffunction>
这样,在调用不存在的方法时,ColdFusion会自动调用onMissingMethod()
函数,并传递方法名和参数。然后,可以在onMissingMethod()
函数中动态调用相应的函数。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云