This is a common question for anyone who starts working with Object Orientation. And, to make matters worse, a lot of people try to explain, but it complicates so much the explanation that the amendment ends worse than the sonnet.
When in doubt about when to extend a class, use an interface (extra credit), or inject another from an object, you should ask yourself if your object is a particular thing, if it > can be a certain thing (besides itself) or it only needs of a certain thing to work.
-
If you respond that a object is something else, you apply an inheritance.
-
If you respond that your object may be something other than itself, implement an interface. Even empty, they serve as types, which is great for polymorphism.
-
If you respond that your only needs another object, inject it as a dependency.
In your particular case, Marketplace is not a Curl object, but it can be , so you should have an interface.
I would suggest, however, something more subjective, since data can come from anywhere, whether local, from a database, or remote. And if remote, you can use Curl or Stream Sockets :
interface OffersDataAccess {
public function getOffers();
}
class Marketplace implements OffersDataAccess {
public function getOffers() {}
}
And whoever is using this class gets a OffersDataAccess which can be a Marketplace as well as a Catalog a CommercialBreak (commercial range) or even an OutdoorAnnouncement (outdoor):
class Catalog implements OffersDataAccess {
public function getOffers() {}
}
class CommercialBreak implements OffersDataAccess {
public function getOffers() {}
}
class OffersController {
public function listOffers( OffersDataAccess $offers ) {
return $offers -> getOffers();
}
}
class OutdoorAnnouncement {
public function listOffers( OffersDataAccess $offers ) {
return $offers -> getOffers();
}
}
$controller = new OffersController;
$marketPlaceOffers = $controller -> listOffers( new Marketplace );
$catalogOffers = $controller -> listOffers( new Catalog );
$commercialBreakOffers = $controller -> listOffers( new CommercialBreak );
$outdoorAnnouncementOffers = $controller -> listOffers( new OutdoorAnnouncement );