假设我有以下组件,名为Base
<cfcomponent output="false">
<cffunction name="init" access="public" returntype="Any" output="false">
<cfset variables.metadata = getmetadata(this)>
<cfreturn this>
</cffunction>
<cffunction name="getmeta" access="public" returntype="Any" output="false">
<cfreturn variables.metadata>
</cffunction>
</cfcomponent>
我想在另一个名为Admin的组件中扩展基础
<cfcomponent output="false" extends="Base">
</cfcomponent>
现在,在我的应用程序中,如果在创建对象时执行以下操作:
<cfset obj = createobject("component", "Admin").init()>
<cfdump var="#obj.getmeta()#">
我得到的元数据告诉我,该组件的名称是Admin,它正在扩展我的Base组件。这很好,但是我不想在创建对象时显式地调用init()方法。
如果我能在我的Base组件中做这样的事情,那就太好了:
<cfcomponent output="false">
<cfset init()>
<cffunction name="init" access="public" returntype="Any" output="false">
<cfset variables.metadata = getmetadata(this)>
<cfreturn this>
</cffunction>
<cffunction name="getmeta" access="public" returntype="Any" output="false">
<cfreturn variables.metadata>
</cffunction>
</cfcomponent>
但是,getmeta()方法返回的元数据告诉我,组件名是Base,尽管它仍在扩展。对如何做到这一点有什么想法吗?
发布于 2008-12-12 12:50:22
你为什么不想在每个扩展的cfc中调用init?
<cfcomponent output="false" extends="Base">
<cfset super.init()>
</cfcomponent>
这似乎像你想要的那样填充元数据。
发布于 2008-12-17 08:20:39
我并不100%确定您想要的是什么,但是ColdFusion 8添加了getComponentMetaData()函数,它不需要实例化的CFC,而是向CFC使用点标记路径。您应该能够从Admin获得路径,您可以传递到getComponentMetaData(),而无需调用init() on Base。
发布于 2014-09-15 08:32:17
6岁,但我会给出真正的答案.
给定Base.cfc:
component{
public function foo(){
return 'base';
}
}
和Child.cfc:
component extends="Base"{
public function foo(){
return 'child';
}
}
要了解哪个组件子扩展,只需执行以下操作:
<cfscript>
child = createObject( "component", "Child" );
writeDump( getMetaData(child).extends.name );
</cfscript>
https://stackoverflow.com/questions/363770
复制