All CFCs inherit component.cfc

{ Posted By : Eric Cobb on July 21, 2010 }
1861 Views
Related Categories: ColdFusion

This is a pretty cool feature built in to ColdFusion that I'll wager many people don't know about. I had certainly never hear of it until this morning. Did you know that ColdFusion has a built in component.cfc component that is automatically inherited by all CFCs on your server? To quote the Adobe documentation:

All CFCs automatically extend the ColdFusion WEB-INF/cftags/component.cfc component. (The WEB-INF directory 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.

Note: When you install a newer version of ColdFusion, the installation procedure replaces the existing component.cfc file with a new version. Therefore, before upgrading, save any code that you have added to the component.cfc file, and then copy the code into the new component.cfc file.

I thought this might be something new in CF 9, but as it turns out it's available as far back as CF 6.

I like the idea of having an automatically inherited CFC, however I'm not that crazy about it being located in the server files instead of the actual application code. I realize it's intended to be more of a global server tool, to be used across all applications running on that server instance, but something about it being "out of site" just bugs me.

What would be really cool is if something like this were available on a per application level, where you can have a Component.cfc just like you do Application.cfc. Whatever is in your Component.cfc automatically gets inherited by all CFCs in your application. This would be great for stuff like error handling functions or setting variables (like datasources) instead of constantly having to pass these in to your CFCs.

So, who do I have to pester to get a feature request in for CF X?

Comments
JF's Gravatar I don't think it's a good thing to have this at the Application layer, it should remain at the server layer just like all objects truly inherit from Java's object class which is also at the server.

It might appear like a convenient place to do global stuff, but really all those things, such as error handling, global variables are better done elsewhere IMHO.
# Posted By JF | 7/21/10 1:42 PM
phill.nacelli's Gravatar "What would be really cool is if something like this were available on a per application level, where you can have a Component.cfc just like you do Application.cfc"

You do, what's stoping you from creating a BaseBean.cfc file that all other cfc's (or just the ones you want! more control already see...) can inherit from?
# Posted By phill.nacelli | 7/21/10 2:41 PM
Eric Cobb's Gravatar @phil - What I was referring to was having a special component that that all of your other components automatically inherited from, without the need manually extend it in every CFC. Kind of like back in the Application.cfm days, where when you set a variable it was available on every page, without having to use cfinclude.
# Posted By Eric Cobb | 7/21/10 9:01 PM