How to get the name of the component that's extending mine in ColdFusion?
Asked Answered
S

3

2

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?

Southeaster answered 12/12, 2008 at 18:49 Comment(0)
R
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.

Runin answered 12/12, 2008 at 20:50 Comment(0)
R
1

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()

Rennarennane answered 17/12, 2008 at 16:20 Comment(0)
A
1

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>
Anticatalyst answered 15/9, 2014 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.