views:

131

answers:

3

From a maintenance and code organization standpoint, in PHP5, does it make sense to create/define objects and classes for XML data coming from a web service?

Using Twitter's API as an example, I would have a class for each API method (statuses, users, direct_messages, etc). For statuses/public_timeline, I would have something like this:

class Statuses {
    public $status = array(); // an array of Status objects
    public function __construct($url) { // load the xml into the object }
}

class Status {
    public $created_at, $id, $text; // and the rest of the attributes follow...
}

$public_timeline = new Statuses('http://twitter.com/statuses/public_timeline.xml');
echo $public_timeline->status[0]->text;

Or is it better to dump everything into an associative array, so items would be accessed like this:

// the load_xml function is just something that will dump xml into an array
$public_timeline = load_xml('http://twitter.com/statuses/public_timeline.xml');
echo $public_timeline['statuses']['status'][0]['text'];

First design:

  • Strictly following object-oriented principles
  • Seems like an approach better suited for compiled languages

Second design:

  • A lot less maintenance would be needed if the API is modified. If the API adds an attribute to the XML, the corresponding class would need to be updated in the first design.
+1  A: 

I think this depends on your project ...

First design:

  • Strictly following object-oriented principles
  • Seems like an approach better suited for compiled languages
  • Necessary in a big application
  • Easy to reuse
  • Benefiting when passing data around
  • You have methods to add functionality, not just data

Second design:

  • A lot less maintenance would be needed if the API is modified. If the API adds an attribute to the XML, the corresponding class would need to be updated in the first design.
  • Straight forward & quick solution
  • Little code
Philippe Gerber
Could you expand on "necessary in a big application"? This would be a library in an MVC application, where the data source is a web service instead of a DB. The models would be the ones calling the library for the data, so I think the reuse/methods to add functionality would be a part of the model.
jimyi
If your application is big (you have a lot of files, classes, functionality, data, etc.) you want to separate those parts from each other for code maintainability and code reuse issues.
Philippe Gerber
+1  A: 

I'm with Philippe: if your application is going to be really small (just calling the status methods for example), go with solution 2.

I agree that creating a bunch of classes just to echo status informations isn't really needed in the first place. But if your application it's going to be huge, design with solution 1 in mind. As you go along with your development, you're going to create specific methods that would belong to specific classes. Sometimes you would like to create a method to "order" status messages. Who knows? That's why we create classes, each one with it's own responsibility, so you wouldn't need to search a big php file with hundreds of funtions.

I do believe that if you don't know how your application is going to grow, a "best-of-both worlds" approach would be creating classes to at least each Twitter categories (Timeline, Status, User, etc, totalizing maybe 12), instead of each method. Is a good solution IMO in your case, if you don't want to create too many classes.

GmonC
A: 

If you're working with XML in PHP5 then I think the best would be to use SimpleXML. Then you have the best of both worlds. You can access your values in a very array-like way. However you can extend SimpleXML class to provide methods and other custom nice-to-haves.

// To get the effect of an array...
$twitte = 'http://twitter.com/statuses/public_timeline.xml';
$public_timeline = simplexml_load_file($twitte);
echo $public_timeline->statuses->status;

Or extend the SimpleXml Class

class MyXml extends SimpleXml
{    
    public function quickStatus()
    {
        $status = $this->xpath("/statuses/status");
        return (string)$status[0];
    } 
}

// then access like
$twitte = 'http://twitter.com/statuses/public_timeline.xml';
$public_timeline = simplexml_load_file($twitte, 'MyXml');
echo $public_timeline->quickStatus();

The above example is just to show how to extend the class. If you'd like more info you can check out the XML class from a library I've created on Google Code

null