1

I'm trying to figure out how to correctly extened several classes without duplicate code. I have the following classes:

  • Base (method A, method B)
  • Invoice : Base (method C)
  • Shipment : Base (Method D)

I have to adjust some functionality without modifying them. The problem is that some methods I need to adjust are in the base class.

If I extend the Base class, I will need to copy Methods C and D. If I extend each of the final classes, then I need to duplicate the changes to methods A, B.

How do I correctly do this without multi-inheritance (PHP, not possible) or duplicate code?

3
  • 1
    Way too broad a question - there are too many possible ways to do this. Traits (multiple inheritance) is one solution, breaking the base class down to require dependencies, and have the two final classes supply different implementations of those dependencies might be a better option. No way to say for sure with such abstract information available Commented Oct 10, 2016 at 11:37
  • I can't change these classes, I can only extend them. They are part of a third party module. Commented Oct 10, 2016 at 11:45
  • Well then traits might well be the best option, again no way to really answer this question though, so it will likely get closed as too broad Commented Oct 10, 2016 at 11:59

1 Answer 1

2

You can use trait to accomplish that.

http://php.net/manual/en/language.oop5.traits.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.