Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion mixin resulting in "Routines cannot be declared more than once."

[Note: it's generally bad practice to include code in your cfcs, (see answers below), so consider this just research]

To summarize, I have a class and a subclass and one method that is overridden by the subclass. When I hard-code the method in the child class, everything works fine, when I use cfinclude to include it in the pseudo constructor, mixin style, I get a "Routines cannot be declared more than once." error.

This seems pretty straightforward. What am I missin' re: this mixin?

parent class:

<cfcomponent >
    <cffunction name="hola" hint="i am the parent method">
        <cfreturn "hola - parent">
    </cffunction>
</cfcomponent>

child class:

<cfcomponent extends="mixinTestParent">
    <!---   this would work, successfully overridding parent method
    <cffunction name="hola" hint="i am the child method">
        <cfreturn "hola - child">
    </cffunction>--->

    <cfinclude template="mixinTestInc.cfm">

    <cffunction name="init" access="public" returntype="any" output="false">
        <cfreturn this>
    </cffunction>
</cfcomponent>

include:

<cffunction name="hola" hint="i am the child method" access="public">
        <cfreturn "hola - child">
    </cffunction> 

runner:

<cfset test = new mixinTestChild().init()>
<cfdump var="#test.hola()#">

thanks in advance!!

like image 433
jbd Avatar asked Dec 11 '25 13:12

jbd


1 Answers

You're getting the error because of the way the the CFC is instantiated.

When you have hola() in the parent & hola() in the child, where the child extends the parent, when the child CFC is created, it sees hola() in the parent and overrides it. However, that function still exists in the CFC.

From the child CFC, you can reference both hola() (defined in the child CFC) and super.hola() (defined in the parent).

When you use <cfinclude/>, the CFC is instantiated and the contents of the included file are added to the mix. However, they aren't seen as part of the inheritance model, just as "other functions in this CFC", so you get the error.

I agree this is a bad practice when done instead of refactoring, but it is a good way to allow utility UDFs into the mix without making them part of your model.

like image 91
Adrian J. Moreno Avatar answered Dec 13 '25 15:12

Adrian J. Moreno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!