A simple story about better php classes
dipl. ing.
class Battery
{
public function supply12v()
{
return 12;
}
}
class BadCar
{
protected $battery;
function __construct()
{
$this->battery = new Battery();
}
public function drive()
{
$this->battery->supply12v();
}
}
class BetterCar
{
protected $battery;
function __construct(Battery $battery)
{
$this->battery = $battery;
}
//...
}
interface BatteryInterface
{
public function supply12v();
}
class BestCar
{
protected $battery;
function __construct(BatteryInterface $battery)
{
$this->battery = $battery;
}
}