http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html?page=1
This is an interesting, but old article. I’d like some opinions from you wise sitepointers.
While I agree with the sentiment, and the purist in me agrees with the ideas behind it, from a practical point of view it seems ridiculous.
Even a simple piece of code like this:
class Car {
protected $manufacturer;
public $wheels = 4;
public $doors = 4;
public function __construct(Manufacturer $manufacturer) {
$this->manufacturer= $manufacturer;
}
}
class HatchBack extends Car {
public $doors = 2;
}
The “extends is evil” people would suggest replacing it with this:
interface Car {
public function __construct(Manufacturer $manufacturer);
}
class GenericCar implements Car {
protected $manufacturer;
public $wheels = 4;
public $doors = 4;
public function __construct(Manufacturer $manufacturer) {
$this->bar = $manufacturer;
}
}
class HatchBack implements Car {
protected $manufacturer;
public $wheels = 4;
public $doors = 4;
public function __construct(Manufacturer $manufacturer) {
$this->bar = $manufacturer;
}
}
The problem is immediately obvious: repeated code. Repeated code is bad because it may change. As I see it, if the interface constructor ever changed then every class which implements would need to be updated to compensate. Using inheritance, only the base class has to be changed and all child classes are instantly effected.
I can appreciate the arguments it makes but the solution of using interfaces is not a good one! This can be achieved with traits too but they still count as tight coupling.