如何获取在ColdFusion中扩展我的组件的名称? [英] How to get the name of the component that's extending mine in ColdFusion?
问题描述
假设我有 Base :
< cfcomponent output =false>
< cffunction name =initaccess =publicreturntype =Anyoutput =false>
< cfset variables.metadata = getmetadata(this)>
< cfreturn this>
< / cffunction>
< cffunction name =getmetaaccess =publicreturntype =Anyoutput =false>
< cfreturn variables.metadata>
< / cffunction>
< / cfcomponent>
,我想在另一个名为 ::
< cfcomponent output =falseextends =Base>
< / cfcomponent>
在我的应用程序中,如果我在创建对象时执行以下操作:
< cfset obj = createobject(component,Admin)。init()>
< cfdump var =#obj.getmeta()#>
我回来的元数据告诉我,组件的名称是 ,并且扩展了我的 基本 组件。这很好,但我不想在创建对象时显式地调用 init() 方法。
如果我可以在 基础 组件中执行类似的操作,我会很高兴:
< cfcomponent output =false>
< cfset init()>
< cffunction name =initaccess =publicreturntype =Anyoutput =false>
< cfset variables.metadata = getmetadata(this)>
< cfreturn this>
< / cffunction>
< cffunction name =getmetaaccess =publicreturntype =Anyoutput =false>
< cfreturn variables.metadata>
< / cffunction>
< / cfcomponent>
然而,getmeta()方法返回的元数据告诉我组件名称是 ,即使它仍在延长。关于如何完成这个任何想法?
6岁,但我会给出真正的答案...
给定Base.cfc:
组件{
public function foo(){
return'base';
}
}
和Child.cfc:
component extends =Base{
public function foo(){
return'child';
}
}
要找出什么组件Child扩展, :
< cfscript>
child = createObject(component,Child);
writeDump(getMetaData(child).extends.name);
< / cfscript>
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?
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>
这篇关于如何获取在ColdFusion中扩展我的组件的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!