Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 CFCs, 2 functions with same name a problem?

Status
Not open for further replies.

stevelionbird

Programmer
Jul 21, 2006
21
US
Hi,

I was going over someone else's code and was wondering if this may pose a problem. (I'm not sure how CF deals with this internally)

Component 1 has a function named FOO.
Component 2 has a function named FOO.

Component 2 extends Component 1.

Will calls to that function simply take priority as to which is defined first up the 'extension ladder' (which component was instantiated), or is this something that should be corrected/avoided. (I hope I don't have to go changing things :-|)

Thanks,
Steve
 
Its called method or function overriding and its perfectly valid.

Let's say you create an instance of Component2

<cfset myComponent2 = createObject("component", "Component2").init()/>
<cfset myComponent2.FOO() />

ColdFusion will call the FOO function defined in Component2. Because Component2.FOO() is said to override the FOO function in the base component (Component1).


HTH
 
Thanks,

I seriously don't think overriding is what this guy had in mind .. just an oversight.

I was hoping there would be some way to refer (from the code where component 2 is instantiated) to component 1's FOO function, but as far as I know you can't. (super keyword doesn't apply there)

Hopefully c1's FOO() won't be needed in places where instances of c2 are, otherwise I would have to either explicitly invoke the c1.FOO() method separately .. or create a 'middle man' function in c2 that essentially acts as c1.FOO() via the super keyword.

Either way, could have been avoided had the functions been named better :\

Thanks for the help,
Steve
 
Steve,

You might be right. It really depends on what the original programmer had in mind.

What do you mean when you say the super keyword doesn't apply? Do you mean that Component2.FOO isn't calling super.FOO()?

If you really needed access to Component1.FOO, I suppose you could add an optional parameter to Component2.FOO that determines which FOO method to execute. Not the best method but if you were really in a bind it might work.
 
The extra 'which FOO' parameter would have to suffice if I need a workaround.

What I meant by the super keyword not applying was that I can't somehow reference component1.FOO() like this:

Code:
objComponent2 = createObject("component", "component2.path");
objComponent2.super.FOO(params);
to use component1's FOO() .. kinda like override-overriding .. er .. underriding? hehe ;)
 
underriding". I like that term!

I see what you mean. You're right. You can't call super from outside the subcomponent (Component2).

Then again, if the components were structured correctly it wouldn't be an issue. But you already expressed your doubts about that ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top