I have to get some information from the database models. The problem is that all the models (that describe the tables) are different. The information of the models must be shown in a calendar, therefore I need the same structure [title, date from, date until, type of event, etc] for all the models.
So my idea is to create an interface that forces the models to have certain characteristics. An example model is this:
Model that represents a table in the database
class EventsType1 extends Model {
// metodos y atributos del modelo 1
}
class EventsType2 extends Model {
// metodos y atributos del modelo 2
}
Interface that I want the models to implement:
interface IScheludable {
// permite obtener los eventos en el formato que la agenda necesita
public function getEvents(fromDate, toDate);
}
Now I can get the models to have the functions that I need to implement regardless of the type of event, in a way similar to this:
class EventsType2 extends Model implements IScheludable {
// metodos y atributos del modelo 2
public function getEvents(fromDate, toDate){
// return eventos desde el modelo
}
}
BUT, I can not make the interface also force the model to deliver a specific data structure for the agenda. This is an example of the structure that I need (and that the agenda returns to the web client):
class Event {
public $title;
public $dateFrom;
public $dateTo;
public $eventColor;
function __construct($title, $date, $someArgs) {
// set params
}
//etc
}
I understand that in PHP 7 you can define the type of return, but I do not use PHP 7 in this project. So I need the interface to force the classes that implement it to return some specified structure, otherwise the interface does not make much sense. Consider the following:
class EventsType2 extends Model implements IScheludable {
// metodos y atributos del modelo 2
public function getEvents(fromDate, toDate){
// Operaciones
return new Event('Evento1', '1-2-2015', $otherArgs);
}
}
This is also valid:
class EventsType2 extends Model implements IScheludable {
// metodos y atributos del modelo 2
public function getEvents(fromDate, toDate){
// Operaciones
return new AnyClass(); // <- Justamente esto quiero que no ocurra
}
}