As said, if you are going to extend the class later, you can always change it.
But if you can avoid using inheritance, you should. It is better to use other templates when designing, if possible.
Basically, what to do is support composition over inheritance and program interfaces, not implementations.
If you allow classes to have one clearly defined goal, you can compose objects, rather than letting them inherit from each other. If you use interfaces rather than inheritance, you are likely to define small and efficient interfaces. Then you will see that inheritance will decrease, and therefore the need for protection will decrease.
Example
interface sound { public function makeSound(); } class bark implements sound{ public function makeSound() { return "Bark!!!"; } } class mew implements sound{ public function makeSound() { return "Mewmew!!!"; } } class forLeggedAnimal { private $sound; public function forLeggedAnimal($sound){ $this->sound = $sound; } public function eat(){ echo $this->sound->makeSound(); } } $cat = new forLeggedAnimal(new mew()); $dog = new forLeggedAnimal(new bark());
I know that this is far from a perfect example. But this illustrates the technique, and you can use it in many ways. For example, you can combine this with various creation patterns to create cats and dogs, it may not be that important to know that the cat is barking or ending. But in any case .. it differs from the need to make a base class, and then expand it with a cat and a dog, and therefore they should make the “sound” method a protected or overriding catering method.
/ Peter
Peter Hagström Jul 26 '09 at 11:08 2009-07-26 11:08
source share