0

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?

Justin
  • 42,716
  • 77
  • 201
  • 296
  • Why do you care about the order of the properties? THere is NO order implied in teh properties of an object, and in this case it shows in alphabetical order, so I suppose you could call it `a_id`, but this seems silly. I am not sure what you are getting at with the second part of your question. What do you mean by `create another Event class under a different model`? You Event class doesn't inherit from Model as it stands now. – Mike Brant Feb 28 '14 at 23:27
  • The class `Event` is global, but really it should be contained under the namespace `ExecutionQueue`, so I can create another model `Event` class later. Does that make sense? – Justin Feb 28 '14 at 23:32
  • Yes, I just wasn't sure what you were getting at the create under a different model part. PHP supports namespacing. Check the link: http://us2.php.net/manual/en/language.namespaces.rationale.php You could nuse something like `namespace executionQueue;` at the beginning of the file and then in global context instantiate events like `new \executionQueue\Event()` – Mike Brant Feb 28 '14 at 23:38

0 Answers0