| Contents > Developing ColdFusion MX Applications > Building and Using ColdFusion Components > Building ColdFusion components > Structuring and reusing code 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.
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.
<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.
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>
<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.
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.
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.
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 |
|
|
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.
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
Comments
No screen name said on Sep 22, 2003 at 5:59 PM :