I am developing a range of post filters for a wordpress site, and I have built the first 4 with a single class.

The final two are different enough in scope to only share a single function (the function for producing the final links) in their classes.

Is it reasonable, either in this instance or in any number of similar hypothetical instances, to extend the original class in order to have that functionality, or is there a better way?

  • I don't understand this question. What do you mean by building things with a class? Are these classes that inherit from something? Wouldn't the class at the top of the hierarchy already have the common function? Oct 5, 2011 at 14:31

2 Answers 2


No, if the two classes are so different that they only share one function, the chances are they are not interchangable - and therefore they would break the liskov substition principle.

Only derive from a base class if it's logical to do so. Sharing a single piece of code is rarely a justification for inheritence :)

In this particular example it sounds like you want to re-use that function via composition instead...

  • 3
    Prefer composition over inheritance! In most cases this will make you life simpler in the long run.
    – Travis
    Oct 5, 2011 at 14:56
  • Composition FTW!
    – back2dos
    Oct 5, 2011 at 15:07
  • Worth noting my answer is basically the same as @Sardathrion, I just linked to the relevent principles :)
    – MattDavey
    Oct 5, 2011 at 15:08

If a class description has the word "and" in it, it should be broken down into two (or more) classes. By the looks of things, you could make that function a member of another class that is referenced by the ones you currently have.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.