Can I define functions in a separate file and then include them in multiple cfc to mimic multiple inheritance?


I asked this previous question regarding multiple inheritance in coldfusion cfcs. Now, I have a question of whether this would work to mimic multiple inheritance in cfcs and what the drawbacks might be.

Suppose I have these 4 classes: Animal, Horse, Bird, Pegasus.

  • Horse and Bird are subclasses of Animal.
  • Pegasus is a subclass of Horse.
  • However, Pegasus flies the same way that an instance of the Bird Class does.

Instead of defining fly() identically twice in the Bird class and the Pegasus class, can I define it in a separate file and include it in the cfc of Pegasus and Bird? Are there any drawbacks to doing this?


    <cfcomponent extends="animal">
       <!--- misc functions run, eat, whinny... --->


<cfcomponent extends="animal">
<cfinclude template="flier.cfm">
       <!--- misc functions hop, eat, chirp... --->


<cfcomponent extends="horse">
<cfinclude template="flier.cfm">


<cffunction name="fly">
<!--- flying action --->

I have tried this code and at first glance, it seems to work for what I'm trying to accomplish, but since it was not a mentioned solution to my previous question about multiple inheritance in CFCs, I would like some feedback as to whether this would be a good way to go or not.

Yes, what you described will work and is a technique used relatively frequently for various reasons.

That said, when you ask questions here on SO it is in your best interest to try things, especially if they are easy to try, before you ask if they will work. Generally, when you come here to ask, you should phrase your questions as:

  • This is my problem
  • This is what I've tried
  • This is the error message I'm getting
  • What am I doing wrong?