--- In php-list@yahoogroups.com, Pete <[EMAIL PROTECTED]> wrote: > > In message <[EMAIL PROTECTED]>, Mladen Milankovic > <[EMAIL PROTECTED]> writes > >On Thursday 20 March 2008 21:27:39 Pete wrote: > >> > >> I would usually use inheritance to make a new class, which I was going > >> to change, so as to keep the original class in place. I don't see how > >> your suggestion would work in my case. > >> > >> I have a classA, and a classB, which both do similar things, but with > >> totally different code. I now want to add FunctionC to both, but keep > >> it "in one place", to allow for code changes in the future. > >> > >> What would I be extending? > > > > > >Hi. > > > >It's not really extending as the keyword suggests, maybe it's better to say > >inheretence. Probably will be more familiar in this maner, if you worked with > >some real OOP languages. > > I have, I was trying to keep it simple. (:-> I used to work with > Borland Delphi in the 90s. > > >You create a class, let's say classC, which holds the FunctionC, and > >everything else that is the same in classA and classB (any other function or > >variable). Then the classA and classB can inherit the properties(variables) > >and methods(functions) from classC > > >In this situation both classA and classB will have the FunctionC which is > >defined in one place which was the point of the whole thing. > > I considered this before writing, but it would be "the tail wagging the > dog", and to me it doesn't make sense when going back to edit the code > in the future. Both classA and classB are not really extensions of the > function, so it wouldn't make logical sense when you read the code. > > Also, both classA and classB are already extended from other classes. > > I think, simplest, is to leave it as it is!
Simplest perhaps, but for maintenance it is the worst. There are many solutions. If PHP had multiple inheritance that would be the cleanest solution. One is to move the common function(s) into a separate class and create an instance variable of that class in classA and classB. Or you could go back to the classes from which classA and classB are derived (lets call them class1 and class2, respectively) and refactor them to inherit from classC. Or create an instance variable of classC in class1 and class2. In any case you need to at least add comments to where you make the changes. If you decide to keep the separate implementations in classA and classB, please do yourself and the next dev a favor and add a comment about the other function in the other class. Nothing like fixing a bug in one place and finding you should have fixed it in another. Good luck, whatever decision you make you will regret somewhere down the line 8-). doug