问题
Let's say I have the following component called 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>
and I want to extend base in another component called Admin:
<cfcomponent output="false" extends="Base">
</cfcomponent>
Now within my application if I do the following when creating the object:
<cfset obj = createobject("component", "Admin").init()>
<cfdump var="#obj.getmeta()#">
The metadata I get back tells me that the name of the component is Admin and it's extending my Base component. That's all good, but I don't want to have to call the init() method explicitly when creating the object.
I would be nice if I could do something like this in my Base component:
<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>
However then the metadata returned by the getmeta() method telling me that the component name is Base even though it's still being extended. Any thoughts on how to accomplish this?
回答1:
Is there a reason you don't want to call init in each extending cfc?
<cfcomponent output="false" extends="Base">
<cfset super.init()>
</cfcomponent>
That seems to populate the metadata like you want.
回答2:
I'm not 100% sure what you are after, but ColdFusion 8 added the getComponentMetaData() function that instead of requiring an instantiated CFC, takes a dot notation path to the CFC. You should be able to get the path from Admin which you can pass to getComponentMetaData() without calling the init() on Base.
ColdFusion LiveDoc: getComponentMetaData()
回答3:
6 years old, but I'll give the real answer...
Given Base.cfc:
component{
public function foo(){
return 'base';
}
}
And Child.cfc:
component extends="Base"{
public function foo(){
return 'child';
}
}
To find out what component Child extends, just do this:
<cfscript>
child = createObject( "component", "Child" );
writeDump( getMetaData(child).extends.name );
</cfscript>
来源:https://stackoverflow.com/questions/363770/how-to-get-the-name-of-the-component-thats-extending-mine-in-coldfusion