VitalyP
VitalyP

Reputation: 1867

PHP wrapper module (for working with many social networks) design

I am not good at OOD yet so it would nice to receive some advices. I am going to write wrapper class for working with many social networks and services through it's APIs. I want my wrapper could hide specifics for every service/social network behind one interface. It should be flexible and easy to reuse.

For example:

$sn = new SocialNetworks();
$sn->post(new Twitter('some post body'));
$sn->post(new Facebook(array('photo'=>'blabla.jpg')));
$sn->post(new Tumblr('long text'))->attach('blabla.jpg');

Well, something like this. So what could be the best design solution for this? Thank you

Upvotes: 2

Views: 167

Answers (2)

Brice Favre
Brice Favre

Reputation: 1527

I think you should think of a factory : http://en.wikipedia.org/wiki/Factory_method_pattern

Maybe something like this :

<?php
class Service
{
    public static function build($type)
    {
        $class = 'Service' . $type;
        if (!class_exists($class)) {
            throw new Exception('Missing format class.');
        }
        return new $class;
    }
}

class ServiceTwitter {}
class ServiceFacebook {}

try {
    $object = Service::build('Twitter');
}
catch (Exception $e) {
    echo $e->getMessage();
}

Upvotes: 0

Srisa
Srisa

Reputation: 1025

You are probably better off defining an interface for all actions and then implement that interface in all the socialnetwork classes. Something along the lines of


interface ISocialNetwork {
   public function post();
}
class Twitter implements ISocialNetwork {
   public function post() {
   }
}
class Facebook implements ISocialNetwork {
   public function post() {
   }
}

$Tw = new Twitter('some post body');
$Tw->post();

Upvotes: 2

Related Questions