Skip to main content
1 of 3
Simon Bergot
  • 8k
  • 5
  • 37
  • 55

I guess that what the book aims at teaching is to avoid things like:

public function cost() { if($this->chargeType == 'fixed rate') { return "30"; } else { return $this->duration * 5; } } 

My interpretation of the book is that you should have three classes:

  • AbstractLesson
  • HourlyRateLesson
  • FixedRateLesson

You should then reimplement the cost function on both subclass.

My personnal opinion

for simple cases like that: don't. It is strange that your book favors deeper class hierarhy to avoid simple conditionnal statements. What's more, with your input spec, Lesson will have to be a factory with a dispatch which is a conditionnal statment. So with the book solution you will have:

  • 3 classes instead of 1
  • 1 inheritance level instead of 0
  • the same number of conditionnal statements

That's more complexity with no added benefit.

Simon Bergot
  • 8k
  • 5
  • 37
  • 55