I am creating some model classes for an API that I'm building in PHP, to give rigid structure to the JSON responses. Here is an example:
class Model {
public $id;
public function __construct($id) {
$this->id = $id;
}
}
class ExecutionQueue extends Model {
public $current_status;
public $events;
public function __construct($id, $current_status, array $events = array()) {
parent::__construct($id);
$this->current_status = $current_status;
$this->events = $events;
}
}
class Event {
public $status;
public $date;
public function __construct($status, $date) {
$this->status = $status;
$this->date = $date;
}
}
$execution_queue = new ExecutionQueue('id_sdjdfV4s33KffsTdaD43', 'QUEUED', array(
new Event("QUEUED", "2014-02-28T20:59:08+0000"),
new Event("RUNNING", "2014-02-28T20:59:08+0000"),
new Event("COMPLETED", "2014-02-28T20:59:08+0000")
));
echo json_encode($execution_queue, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
Which results in the following JSON response:
{
"current_status": "QUEUED",
"events": [
{
"status": "QUEUED",
"date": "2014-02-28T20:59:08+0000"
},
{
"status": "RUNNING",
"date": "2014-02-28T20:59:08+0000"
},
{
"status": "COMPLETED",
"date": "2014-02-28T20:59:08+0000"
}
],
"id": "id_sdjdfV4s33KffsTdaD43"
}
I have a few questions. First is it possible to have the id
returned above current_status
and events
in the JSON response? Second, how can I nest the Event
class under ExecutionQueue
so I don't pollute the global class name space, i.e. if I want to create another Event
class under a different model? Finally, anything that can be improved and cleaned up and made more object oriented?