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 personnalpersonal opinion
for simple cases like that: don't. It is strange that your book favors deeper class hierarchy to avoid simple conditionnalconditional statements. What's more, with your input spec, Lesson will have to be a factory with a dispatch which is a conditionnal statmentconditional statement. So with the book solution you will have:
- 3 classes instead of 1
- 1 inheritance level instead of 0
- the same number of conditionnalconditional statements
That's more complexity with no added benefit.