View comments | RSS feed
Contents > Developing ColdFusion MX Applications > Building and Using ColdFusion Components > Building ColdFusion components > Structuring and reusing code Using inheritance and the Super keyword PreviousNext

Using inheritance and the Super keyword

Component inheritance and the Super keyword are two important tools for creating structured, object-oriented ColdFusion components.

The following sections describe these coding techniques in greater detail.

Using component inheritance

Component inheritance lets you import component methods and properties from one component into another component. Inherited components share any component methods or properties that they inherit from other components, and ColdFusion MX initializes instance data in the parent CFC when you instantiate the CFC that extends it.

When using component inheritance, inheritance should define an is a relationship between components. For example, a component named president.cfc inherits its methods and properties from manager.cfc, which inherits its methods and properties from employee.cfc. In other words, president.cfc is a manager.cfc; manager.cfc is an employee.cfc; and president.cfc is an employee.cfc.

When CFC B extends CFC A, CFC A is called the base or parent component; CFC B is called the the sub or child component.

Note: When you use a component that extends another component, the This scope is not available in the base (parent) component. Therefore, any component that you extend must not access the contents of the This scope. However, a component that extends another component, and is not itself extended, has full access to the This scope.

To use component inheritance:

  1. Open the corpQuery.cfc file, and modify the code so that it appears as follows, or create a new corpQuery.cfc with the following contents:
    <cfcomponent extends="appResources.components.tellTime">
       <cffunction name="getEmp" returnType="query">
       <cfargument name="lastName" required="yes">    
           <cfquery name="empQuery" datasource="ExampleApps" dbtype="ODBC">
              SELECT LASTNAME, FIRSTNAME, EMAIL
              FROM tblEmployees
             WHERE LASTNAME LIKE '#arguments.lastName#'
           </cfquery>
           <cfif empQuery.recordcount LT 1>
              <cfthrow type="noQueryResult" 
                message="No results were found. Please try again.">
             <cfelse>
                  <cfreturn empQuery>
             </cfif>
       </cffunction>
    </cfcomponent>
    

    In the example, the cfcomponent tag's extends attribute points to the tellTime component.

  2. If you do not already have a tellTime.cfc with a getLocalTime method, create one with the following contents, in the same directory as the corpQuery.cfc:
    <cfcomponent>
       <cffunction name="getLocalTime">
          <cfoutput>#TimeFormat(now())#</cfoutput>   
       </cffunction>
    </cfcomponent>
    
  3. Create a ColdFusion page with the following code, and save it as inherit.cfm in your web root directory:
    <cfinvoke component="corpQuery" method="getEmp" lastName="gilson">
    <cfinvoke component="corpQuery" method="getLocalTime">
    

When you execute the inherit.cfm file, the getLocalTime component method executes even though it is invoked using the corpQuery component.

Using component.cfc

All CFCs automatically extend the ColdFusion WEB-INF\cftags\component.cfc component. (WEB-INF is in the cf_root/wwwroot directory on ColdFusion configured with an embedded J2EE server. It is in the cf_root directory when you deploy ColdFusion on a J2EE server.) This CFC is distributed as a zero-length file. You can use it for any core methods or properties that you want all CFCs in your ColdFusion application server instance to inherit.

Using the Super keyword

You use the Super keyword only on CFCs that use the Extends attribute to extend another CFC. Unlike ColdFusion scopes, it is not used for variables; it is only used for CFC methods, and it is not available on ColdFusion pages that invoke CFCs.

The Super keyword lets you refer to original, base component, versions of methods that the current component extends. Therefore if CFC B extends CFC A, and CFC B overrides some of the methods in CFC A, CFC B can use the original versions of the overridden methods, as defined in CFC A, by prefixing the method name with Super.

For example, assume the following:

With these functions, in CFC B, you can invoke Replace(StringVar="some str%^ing") to replace invalid characters in your text. CFC B could call Super.Replace(StringVar=Arguments.StringVar) to remove the spaces before removing other invalid characters.

Included pages can use the Super keyword.

Super keyword limitations

The following limitations apply to the use of the Super keyword:


Contents > Developing ColdFusion MX Applications > Building and Using ColdFusion Components > Building ColdFusion components > Structuring and reusing code Using inheritance and the Super keyword PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


No screen name said on Sep 22, 2003 at 5:59 PM :
It seems you cannot pass named
arguments to the super. For example,
super.myFunc(arg1="a",arg2="b").
halL said on Sep 23, 2003 at 10:34 AM :
There is a typographical error in the second item in the bulleted list under "Using the Super keyword"
The word Remove should be Replace;
that is, both methods should have the name Replace.
halL said on Sep 26, 2003 at 7:51 AM :
This is a known problem, ColdFusion bug 53153:
When calling methods via the "super" keyword in a child component you can't use named arguments (or argumentCollection) in the method called in super.
You must use ordered arguments.
sanaullah said on Oct 23, 2003 at 3:09 AM :
i am wondering you are talking about ( Super Keyword ). Where is the example that actually do some stuff. Honestly so poorly documentation of CFMX. If you extend your CFC and try to have a constructor use super.init() this does not behave properly, some time you cannot use even super.init() . try to call this CFC in cfm page, you will get errors or mehod could not found like that. Be rational now, always provide advance level examples where most of stuff start breaking up.
jrunrandy said on Oct 24, 2003 at 11:28 AM :
This page says "When you use a component that extends another component, the This scope is not available in the base (parent) component." However, this is incorrect. The This scope _is_ available in the base (parent) component.
eblackey said on Oct 7, 2004 at 10:18 AM :
I have found what looks to be a workaround for the super bug. If you add <cfset this.super = super> to the top of your cfcomponent you can use cfinvoke to call the super methods and pass argumentcollection.

<cfcomponent>
<cfset this.super = super>
<cffunction name="add">
<cfinvoke method="this.super.add" argumentcollection="#argumentcollection#" returnvariable="intid"/>
<cfreturn intid>
</cffunction>
</cfcomponent>
eblackey said on Oct 7, 2004 at 11:13 AM :
Actually that should have been #arguments# instead of #argumentcollection#
lanfeustdetroy said on Jan 17, 2005 at 9:11 AM :
to avoid any problem with the availability of this in the base component
add this in the component.cfc ( restart coldfusion is needed)
<cfcomponent>
</cfcomponent>
and create an empty.cfc with the same code.
extend any cfc with the empty.cfc and that's it no more problem

so if you have CFCA<-CFCB-<CFCC-<CFCD with init method that return
this that will work. try <cfdump var="#this#" /><cfabort /> to be sure.
lanfeustdetroy said on Jan 17, 2005 at 9:12 AM :
sorry it' s super.init()

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/buildi52.htm