Is ORM abstraction a pipe dream?

Abstraction layers in your projects allow for extreme flexibility. That's a given. Define a QueueInterface and create implementations for Beanstalkd, Amazon SQS, and IronMQ. Swap them out without touching anything but your IoC container. That's awesome.

I was recently introduced to the repository pattern, a type of abstraction and organizational technique. The idea being, create a repository for each of your models to retrieve and persist to and from. A supposed benefit of the repository pattern is the ability to abstract your ORM and create different implemenations for Eloquent, Doctrine, Propel, etc. This abstraction intrigued me. I set off to put this idea into practice and see what it took. Here are my findings.

The goal

My goal for this project was to abstract my ORM. Pretty straightforward, right? Just take the repository pattern to the extreme. Way easier said than done.

Heads up! I'm using Laravel as my framework of choice.

Time to go to work

I decided my first task was to create a UserRepository interface. An interface that had method stubs like create(array $fieldValues), findById($id), and currentUser(), etc.

Next up was to create my first implementation. Because Laravel is my framework, it made sense to implement the Eloquent ORM first. That was pretty easy, but then I realized something...my EloquentUserRepository is going to return Eloquent models. Okay...need to create a UserInterface.

Looks like I need to abstract Eloquent's sexy dynamic setter/getter action. I created my UserInterface with setEmail($email), getEmail(), setPassword($password), and getPassword(), save() methods.

Persisting for ORM X

At this point I started thinking I should peek at some other ORMs to see how they do things. Doctrine was in my line of sights. As soon as I learned about Doctrine's EntityManager I knew architecting this thing was going to be difficult.

In Doctrine, you wouldn't typically call save() on your models. You use the EntityManager to persist($model) and flush() the models. This wraps the save in a SQL transaction for you. I like that a lot. It means transactions are handled for you when you do something like the following.

$user = new User();
$user->setName('David');

$profile = new UserProfile();
$profile->setLocation('Dallas');
$profile->setOccupation('Software Developer');
// relate this profile to the user
$profile->setUser($user);

$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->persist($profile);
$em->flush();

I want to use transactions with Eloquent, so it looks like I need to add a save(UserInterface $user) method to the UserRepository interface. I'll just call save() on the Eloquent model inside of the EloquentUserRepository's save(UserInterface $user) method.

Hmm...what about relationships?

After looking at Doctrine, I realized I needed to create different method stubs somewhere for relating and un-relating models.

I wonder how Propel handles relationships. Hmm...looks like you set related models on each other and then save() it, which cascades the saves.

//Propel relationship saving
$author = new Author();
$author->setFirstName("Leo");
$author->setLastName("Tolstoy");
// no need to save the author yet

$publisher = new Publisher();
$publisher->setName("Viking Press");
// no need to save the publisher yet

$book = new Book();
$book->setTitle("War & Peace");
$book->setIsbn("0140444173");
$book->setPublisher($publisher);
$book->setAuthor($author);
$book->save(); // saves all 3 objects!

About that...do I create a UserRelationship interface? Do I add methods to the UserInterface? How should I go about handling the cascading saves by Propel versus the immediate saves done by Eloquent versus the flushing done by Doctrine? Uhhh...I'll just not worry about that for now. Validation is what I need next!

Validation

I like the idea of validating data for models before I stick the data inside the model. In Laravel, I can just create a create a class specifically for validation and use Laravel's validation class. I'll do that and validate an array of POST data from the request.

Just to be safe, I better take a peek at how Doctrine handles validation. Umm...a bit different. Looks like I can get a handle on a validator object and pass it a model or I can create a some kind of form object from my models and validate it by passing it a Request object. Looks like a need to create interfaces for each of my form validators and have different implementations for each ORM. That sucks.

Wonder how Propel handles validation. Looks like you validate a Propel model by calling a validate() on the model itself. Great...so Laravel validates an array of data, Doctrine a Request object, and Propel the models themselves. How do I interface that?

Observers

I think I want to be able to hook into different events and have event listeners for certain actions. Eloquent already has some Eloquent events defined for saving, updating, deleting models. That's nice but no good for me. I need generic events being fired.

I need to fire those same events in all of my UserRepository implementations. Yet another thing to keep in mind.

This is rough

Geez, I had no idea how much planning this would take. I'm not getting anywhere. Although a cool idea, this proof of concept isn't worth the time I'm spending on it.

What do you have to show for this?

Not much! :( I wish I had some cool demo where I could change a flag in the code and Eloquent would be used, change it again and Doctrine would be used, change it again and Propel would be used...but, alas, I do not.

The truth is I've spent so much time reading about how these different ORM's handle different aspects and not writing any code. After learning how different each ORM is, I've found it incredibly difficult to interface the different components to make an ORM agnostic project work.

I do not want to sound like I'm whining about something I was promised by using the repository pattern. That is not the intention of this post at all. The repository pattern most definitely has its benefits, but completely abstracting your ORM was a little too difficult for me. It was hindering me from actually producing anything.

It's not all doom and gloom for me though. This forced me to take a look at Doctrine(which I'm liking the more I see) and Propel. Learning how these different ORM's handle validation, relationships, transacations, etc has been a great experience for me. It has helped me form some opinions about what I like and don't like in an ORM, which is great. I didn't even think about that indirect benefit when I started.

As of right now, for me, I've decided that I can use the repository pattern for organizational purposes, but not ORM abstraction. I think I need to pick an ORM and stick with it to get anything done.

Overall I'm really glad I attempted this. I learned a lot. I hope this post starts a discussion on proper ORM abstraction. I would love to here your guys' thoughts on the subject.

Tags: PHP, Intermediate

We Need Some Closure

Heard of anonymous functions, Closures, or lambda functions? Seen some php that looks kinda like jQuery? Anonymous functions confused the crap outta me at first. Let's try to add these concepts to your programming toolbelt.

Mary had a little lambda

Anonymous functions are nothing more than functions you can assign to a variable. Consider the following example.

function add($x, $y)
{
    return $x + $y;
}

$subtract = function($x, $y) {

    return $x - $y;
};

echo add(2, 3); // "5"

echo $subtract(8, 2); // "6"

The first function declaration should look familiar. Nothing fancy going on here. The second function is the interesting one. $subtract is what's called a lamda function. It is a function that has no name. If we assign it to a variable, we can invoke it just like a normal function, passing parameters in via the paranthesis. Crazy name, not-so-crazy to learn!

Time for Closure

So what the heck is a Closure? It's a type of lambda function that is aware of its surroundings. Now that we know what a Closure is let's move on...just kidding! Let's look at an example.

$multiplier = 10;

$multiply = function($x) use ($multiplier) {

    return $x * $multiplier;
};

echo $multiply(1); // "10"
echo $multiply(4); // "40"
echo $multiply(10); // "100"

A Closure is an anonymous function that you can pass variables to remember and use later. In the above example, the Closure is remembering the $multiplier variable. It uses it on all future invocations.

I refer to all anonymous functions as Closures. So for the remainder of this post, I'll use the term Closure interchangeably with lambda function and anonymous function.

While these examples are extremely simple and not very useful, it introduces a very powerful feature in php. Let's continue on to see how we can make these ideas a little more useful for us.

Knowledge! Closures can have any number of arguments and use variables.

When are these things useful?

These are neat but it's difficult to figure out how these can be useful at first glance. Let's set up a scenario to utilize Closures.

Let's pretend we have a list of objects that we need to make sure all pass some sort of validation. Fairly common scenario, right? First, we'll define some requirements for this validation class:

  • Can add objects to be validated
  • Can add custom validation rules
  • Can validate all objects at once using custom validation rules

To satisfy these requirements, we could code the following class.

class Validator
{
    /**
     * Objects to be validated
     *
     * @var array
     */
    protected $objects = [];

    /**
     * Validation rules to use
     *
     * @var array
     */
    protected $rules = [];

    /**
     * Errors generated for the validation attempt
     *
     * @var array
     */
    protected $errors = [];

    /**
     * Add an object to be validated
     *
     * @param $obj
     * @return void
     */
    public function addObject($obj)
    {
        $this->objects[] = $obj;
    }

    /**
     * Add a validation rule
     *
     * @param callable $rule
     */
    public function addRule(Closure $rule)
    {
        $this->rules[] = $rule;
    }

    /**
     * Validate all objects in the validator
     *
     * @return bool
     */
    public function validate()
    {
        //empy the errors array
        $this->errors = [];

        //loop through all objects to be validated
        foreach ($this->objects as $object) {

            //loop through all rules to check against the object
            foreach ($this->rules as $rule) {

                //run the rule against the current object
                $rule($object);
            }
        }

        //if there are 0 errors, the validation passes and returns true, returns false otherwise
        return count($this->errors) == 0;
    }

    /**
     * Add an error to the validator
     *
     * @param string $key
     * @param string $error
     */
    public function addError($key, $error)
    {
        $this->errors[$key][] = $error;
    }

    /**
     * Get all errors generated
     *
     * @return array
     */
    public function getErrors()
    {
        return $this->errors;
    }
}

Well that looks cool and all, but there seems to be something missing. The validate() method isn't adding any errors when checking the rules, but it's relying on the count of the errors to determine if the validation passes. This is clearly broken...or is it? Let's actually use our Validator and see it in action.

//create our validator object
$validator = new Validator;

//define a rule
$validPhone = function($person) use ($validator) {

    //strip all non-integer characters
    $phone = preg_replace("/[^0-9]/", "", $person->phone);

    if (strlen($phone) !== 10) {

        $validator->addError($person->name, "Invalid phone number.");
    }
};

//add the rule to the validator
$validator->addRule($validPhone);

//define and add a valid email rule in one go
$validator->addRule(function($person) use ($validator) {

    if ( ! filter_var($person->email, FILTER_VALIDATE_EMAIL)) {

        $validator->addError($person->name, "Invalid email.");
    }
});

//define and add a valid name rule in one go
$validator->addRule(function($person) use ($validator) {

    if (strlen(trim($person->name)) == 0) {

        $validator->addError($person->name, "No name.");
    }
});

//let's create some objects to validate
$cory         = new stdClass;
$cory->name   = "Cory";
$cory->phone  = "123-456-7890";
$cory->email  = "cory@";

$shawn         = new stdClass;
$shawn->name   = "Shawn";
$shawn->phone  = "456-45-2";
$shawn->email  = "shawn";

$topanga        = new stdClass;
$topanga->name  = "Topanga";
$topanga->phone = "789-012-3456";
$topanga->email = "[email protected]";

//add the objects to be validated
$validator->addObject($cory);
$validator->addObject($shawn);
$validator->addObject($topanga);

In the above code, we're defining three rules: has valid phone, has valid email, and has name.

When defining the first rule, we're assigning it to the $hasPhone variable and then adding it the validator with the addRule() method. Our Closure takes one argument, a $person object. It uses the $validator object. By using the same Validator instance, the Closure has access to it every time it gets invoked. In this example, we're using it to add errors.

Remember! It's important to note that unlike function arguments, variables being passed in the use parenthesis must already exist outside of the Closure.

The other rules, valid phone and valid email, aren't being assigned to variables. We're skipping that part and defining them right in the addRule() method of the Validator. Both ways are completely valid. jQuery advocates heavy use of this technique. It's the same idea.

Now that we've set up our Validator, we can run it and see what we get.

// run the validation
$isValid = $validator->validate(); //false

//dump the results
var_dump($validator->getErrors());

/*
    array (size=2)
      'Cory' =>
        array (size=1)
          0 => string 'Invalid email.' (length=14)
      'Shawn' =>
        array (size=2)
          0 => string 'Invalid phone number.' (length=21)
          1 => string 'Invalid email.' (length=14)
*/

As expected, our errors are assigned to the appropriate names. Awesome!

Why not create a class?

It's ideal to create classes that you can reuse multiple times in various places. However, sometimes you just know that some code will only ever run once and it's not worth creating a whole class or multiple classes to fill the requirement. Closures fit the bill perfectly. All Closures are objects in php. They allow us to adhere to object-oriented principles with extreme flexibility without requiring classes to be created for absolutely everything.

We could have tackled this another way. We could have defined a RuleInterface with check($object) and getError() methods. Then, we could have created classes that implement this interface for each rule. In this case, that'd be three separate classes. We could then create those rule objects and pass them to a Validator object that would check the rules against the objects being validated. If the rule check fails, get the error from the rule object and add it to the Validator's errors.

Symfony 2 actually uses this strategy for validating form input. It makes sense for common rules that will be used multiple times.

Our contrived example makes a little more sense to use Closures because we're validating "person" objects and maybe we never need to run this same combination of rules against a list of people anywhere else in our application.

Containers

Nowadays, it's very common to see Closures being used for containers. These containers' purpose is to create objects and inject their dependencies so we don't have to do that every single time. Consider the following example.

$container = [];

$container['EmailTemplate'] = function(){

    return new EmailTemplate;
};

$container['DataSource'] = function(){

    return new MySQLDataSource;
};

$container['NewsLetterSender'] = function() use ($container) {

    //used to created emails
    $template   = $container['EmailTemplate']();

    //used to track stats about the sending
    $dataSource = $container['DataSource']();

    return new NewsLetterSender($dataSource, $template);
};

$newsLetterSender = $container['NewsLetterSender']();

//versus the more verbose and less flexible

$newsLetterSender = new NewsLetterSender(new EmailTemplate, new MySQLDataSource);

As you can see, the $container's only responsibility is to create and assemble new objects. This concept is known as Inversion of Control. It's so popular because it's an elegant way to encourage dependency injection. While both of those concepts are out of the scope of this post, they are worth mentioning because you will most likely see this idea used elsewhere.

Learning is fun! Check out net tut's post about dependency injection and the IoC container!

To Closure this post

Anonymous functions are a sweet feature in php. Although a little hard to conceptualize their use, they can be quite handy. I hope I've been able to shed a little light on these unique functions. I highly encourage you to toy around with them. If you're just starting to learn php, you're bound to see them used more and more.

Homework! As practice, create a Sorter class that you can pass sort Closures instead of validation Closures to sort objects into an array.

**Hint!** Instead of a $this->errors instance variable, change it to $this->sorted and instead of addError($key, $error), change it to sortObject($category, $object).

Tags: PHP, Beginner

Abstract classes, abstract methods, interfaces, what?

If you've been developing with PHP for any length of time you've probably heard of concrete classes, abstract classes, abstract methods, and interfaces. Many of the popular frameworkd encourage you to "program to an interface", but what does that mean? And when should you program to an abstract class, or define an abstract method? Let's figure this out.

What's what?

I could give you definitions, but most people have a hard time wrapping their heads around these ideas without examples. I'd like for us to incrementally learn with a "real" life example. You won't find any car, animal, or whatever weird metaphors you have to figure out how to translate to real use cases here.

Starting small

First things first, let's define a small, achievable goal. Our application needs to be able to email its users. Let's accomadate that. We would probably write a class like this.

class BasicEmail 
{
    private $email;

    private $body;

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function setBody($body)
    {
        $this->body = $body;
    }

    protected function isValid()
    {
        return is_email($this->email) && strlen($this->body) > 0;
    }

    public function attempt()
    {
        if($this->isValid())
        {
            $this->send();
        }
    }

    public function send()
    {
        //use $email and $body to send email
    }
}

//elsewhere in your application

$email = new BasicEmail;
$email->setEmail('[email protected]');
$email->setBody('Thanks for joining CatLoverApp, the social media platform to show off your cat!');
$email->attempt();


Sweet. That looks good. We're using object oriented programming! Let's continue on.

Your users are really enjoying getting emails when their friends post pictures of their cats. Things are good, except many users are requesting SMS messages to be sent in addition to emails. A service like Twilio is perfect for this. Let's use it.

class SMS 
{
    private $phone;

    private $text;

    public function setPhone($phone)
    {
        $this->phone = $phone;
    }

    public function setText($text)
    {
        $this->text = $text;
    }

    protected function isValid()
    {
        return is_phone($this->phone) && strlen($this->text) > 0;
    }

    public function attempt()
    {
        if($this->isValid())
        {
            $this->send();
        }
    }

    public function send()
    {
        //use Twilio to send SMS
    }
}

//elsewhere in your application

$sms = new SMS;
$sms->setPhone('123-456-7890');
$sms->setText('Bob, just posted a pic of his cat!');
$sms->attempt();


Abstract classes and methods

Cool, now we can send emails and SMS messages to our users...but our classes look very similar. That doesn't feel quite right. They both have a destination, text body, isValid(), attempt(), and send() methods. This is the perfect scenario for an abstract class. That could look like this.

abstract class Message 
{
    private $destination;

    private $content;

    public function setDestination($destination)
    {
        $this->destination = $destination;
    }

    public function setContent($content)
    {
        $this->content = $content;
    }

    public function attempt()
    {
        if($this->isValid()) {
            $this->send();
        }
    }

    abstract protected function isValid();

    abstract public function send();
}

class BasicEmail extends Message 
{
    protected function isValid()
    {
        return is_email($this->destination) && strlen($this->content) > 0;
    }

    public function send()
    {
        //use $this->destination and $this->content to send email
    }
}

class SMS extends Message 
{   
    protected function isValid()
    {
        return is_phone($this->destination) && strlen($this->content) > 0;
    }

    public function send()
    {
        //use $this->destination and $this->content to send SMS
    }
}

//

$message = new Message; //Invalid, won't work because it's abstract!

$message1 = new BasicEmail;
$message1->setDestination('[email protected]');
$message1->setContent('Sorry, Susan just de-friended you!');
$message1->attempt();

$message2 = new SMS;
$message2->setDestination('123-456-7890');
$message2->setContent('Sorry, Susan just de-friended you!');
$message2->attempt();


We can use abstract classes when we have similar objects that share some functionality but do things a bit differently. In our Message class we've defined a couple abstract methods. We are forcing a requirement on all classes that extend Message. We require them to implement send() and isValid() methods. The Message class doesn't care how exactly it sends the message or how it gets validated, just that it has the ability to do so.

It's also important to note that abstract classes and interfaces(which we'll get to later) can define methods with any type of visibility, public, protected, or private. In these classes, the isValid() should probably be public. I'm merely illustrating a point.

Warning! You can not instantiate an abstract class. They're too vague. They're meant to be extended.
Note! If a class has an abstract method defined, it must be an abstract class. However, it is still valid to define an abstract class that has zero abstract methods. It's uncommon, but valid.

Message queue

We can't predict the future and can't know what our users will want next. Maybe an actual phone call, who knows? We do know that they like notification options and want lots of them. Let's put together some kind of message queue. Our first pass might look like this.

$messages = array();

$messages[] = $message1;
$messages[] = $message2;

/** other application code **/

foreach ($messages as $message) {
    $message->attempt();    
}


Hmm...this works, but it could be a lot better. What happens if our coworker, Norm, sees $messages and adds $messages[] = "You've been awarded a badge!"? Things break. He might not have seen the portion of code that sends all of the messages and expects them to have an attempt() method. For our and our coworkers sake, let's refactor. Let's create an actual MessageQueue.

class MessageQueue 
{
    private $messages = array();

    public function add(Message $message)
    {
        $this->messages[] = $message;
    }

    public function sendAll()
    {
        foreach ($this->messages as $message) {
            $message->attempt();
        }
    }
}


This looks a lot better. We're even type-hinting the add() method! Our coworkers will know that the add() method requires only Message objects(which BasicEmail and SMS are).

Extending functionality

I'm liking this MessageQueue idea. It doesn't care what kind of Message it's sending, just that it needs to send them. This is great.

Now, let's say we want to really encourage our users to use our service. Let's reward them 5¢ for every cat photo they post.

class PhotoReward 
{
    private $amount;

    private $recipient;

    public function setAmount($amount)
    {
        $this->amount = $amount;
    }

    public function setRecipient($recipient)
    {
        $this->recipient = $recipient;
    }

    public function reward()
    {
        //use paypal to send reward to recipient
    }
}


This doesn't qualify as a Message but it would be great if we could use it in a queue. Time for an interface! An interface imposes requirements on our classes in the form of methods, just like abstract methods, only there's zero implementation code.

One advantage to using interfaces is that you can bolt them on to your existing classes. A class can only extend a single other class. It can only have one parent. When it comes to interfaces, however, classes can implement an unlimited number of interfaces.

Fun Fact! Interfaces can be extended by other interfaces. I don't think I've seen it done out in the wild, but it can be. Usually it's clearer to just have your classes implement multiple interfaces.

Let's define an Event interface.

interface Event 
{
    public function fire();
}


As you can see, the interface does nothing but define a fire() method. Any class that uses/implements this interface will be required to define a fire() method.

Time to refactor our queue class. Let's genericize it a bit by using the Event interface.

class EventQueue 
{
    private $events = array();

    public function add(Event $event)
    {
        $this->events[] = $event;
    }

    public function fireAll()
    {
        foreach ($this->events as $event) {
            $event->fire();
        }
    }
}


Now, let's refactor our other Message and PhotoReward classes to use the interface.

abstract class Message implements Event 
{
    private $destination;

    private $content;

    public function setDestination($destination)
    {
        $this->destination = $destination;
    }

    public function setContent($content)
    {
        $this->content = $content;
    }

    public function attempt()
    {
        if($this->isValid()) {
            $this->send();
        }
    }

    public function fire()
    {
        $this->attempt();
    }

    abstract protected function isValid();

    abstract public function send();
}

class PhotoReward implements Event 
{
    private $amount;

    private $recipient;

    public function setAmount($amount)
    {
        $this->amount = $amount;
    }

    public function setRecipient($recipient)
    {
        $this->recipient = $recipient;
    }

    public function fire()
    {
        $this->reward();
    }

    public function reward()
    {
        //use paypal to send reward to user
    }
}


Lookin good. There are few things to note.

You'll notice that we didn't need to touch the BasicEmail and SMS classes. We set the interface on their parent abstract Message class. The new fire() method just wraps our attempt() method. This may seem redundant, but we are now able to call these classes Events.

We could have kept the interface on Message and left off the fire() method because Message is abstract. As long as the implementations, BasicEmail and SMS, implemented their own fire() methods, they would adhere to the interface. All abstract requirements are eventually funnelled down to their concrete implementations.

We've implemented an interface on an abstract and a concrete class. Both are valid.

Let's see what we can do now.

$queue = new EventQueue;

$event1 = new BasicEmail;
$event1->setDestination('[email protected]');
$event1->setContent('Sorry, Susan just de-friended you!');

$queue->add($event1);

$event2 = new SMS;
$event2->setDestination('123-456-7890');
$event2->setContent('Sorry, Susan just de-friended you!');

$queue->add($event2);

$event3 = new PhotoReward;
$event3->setRecipient($bob);
$event3->setAmount(5);

$queue->add($event3);

$queue->fireAll();


Because each object adheres to the Event interface, we can add them to the queue and the queue can count on all of its objects to have a fire() method. Sweet.

Inception

Prepare yourselves. We're about to get crazy.

Let's add some dimension to our EventQueue. You'll notice we have a fireAll() method. This class would also make sense to be an Event. It's time to really take advantage of some object oriented programming.

Our new and improved EventQueue could look like this.

class EventQueue implements Event 
{
    private $events = array();

    public function add(Event $event)
    {
        $this->events[] = $event;
    }

    public function fire()
    {
        foreach ($this->events as $event) {
            $event->fire();
        }
    }
}


What does this buy us? Organization and options.

$messageQueue = new EventQueue;
$rewardQueue  = new EventQueue;

$email = new BasicEmail;
$email->setDestination('[email protected]');
$email->setContent('Congrats, you\'re the cat lover of the month!');

$messageQueue->add($email);

$sms = new SMS;
$sms->setDestination('123-456-7890');
$sms->setContent('Congrats, you\'re the cat lover of the month!');

$messageQueue->add($sms);

$photoReward = new PhotoReward;
$photoReward->setRecipient($bob);
$photoReward->setAmount(5);

$rewardQueue->add($photoReward);

$badgeReward = new BadgeReward;
$badgeReward->setRecipient($spencer);

$rewardQueue->add($badgeReward);

$masterQueue = new EventQueue;
$masterQueue->add($messageQueue);
$masterQueue->add($rewardQueue);

//fire events in both queues
$masterQueue->fire();

//or only fire events in $messageQueue
$messageQueue->fire();

//or only fire events in $rewardQueue
$rewardQueue->fire();


We can now treat and handle each queue separately or all at once. It's now possible to put queues inside of queues inside of queues. Dominic Cobb would be proud.

A side effect of using abstract classes and interfaces is having very modulized and extendable code. To create a new Message we just need to extend the Message class and implement send() and isValid() methods.

Need another example?

Let's pretend we have some documents we need to save to a database. We could have the following abstract class.

abstract class Document 
{
    protected $datasource;

    public function __construct(DataSource $datasource)
    {
        $this->datasource = $datasource
    }

    public function save()
    {
        if($this->isNew())
        {
            return $this->datasource->insert($this);
        }

        return $this->datasource->update($this);        
    }

    public function delete()
    {
        return $this->datasource->delete($this);
    }

    public function set(array $attr)
    {
        //set attributes on the document
    }

    public function isNew()
    {
        //return true or false
    }

    public function getTable()
    {
        return static::TABLE;
    }
}

class Person extends Document 
{    
    const TABLE = "people";
}

class Company extends Document 
{    
    const TABLE = "companies";
}
Gripe! One feature I find lacking from php is the concept of final variables. Let me pick on Laravel a bit to illustrate this point.

In the previous code snippet, we refer to a DataSource. We should definitely define a DataSource interface and code implementations to that.

interface DataSource 
{
    public function insert(Document $document);

    public function update(Document $document);

    public function delete(Document $document);

    public function read($id, Document $document);
}

class MySQLDataSource implements DataSource 
{    
    public function insert(Document $document)
    {
        //use mysql specific code to insert a document
    }

    public function update(Document $document)
    {
        //use mysql specific code to update a document
    }

    public function delete(Document $document)
    {
        //use mysql specific code to delete a document
    }

    public function read($id, Document $document)
    {
        //use mysql specific code to read a document
    }
}

class FileSystemDataSource implements DataSource 
{
    //implement DataSource for file system
}

class RedisDataSource implements DataSource 
{
    //implement DataSource for Redis
}

class MongoDBDataSource implements DataSource 
{
    //implement DataSource for MongoDB
}

By programming to an interface, we can extremely easily swap out components. Consider the following example.

$john = new Person(new MySQLDataSource);
$john->set(["name" => "John Doe"]);
$john->save();

$bob = new Person(new RedisDataSource);
$bob->set(["name" => "Bob Smith"]);
$bob->save();

Because we've defined an interface and created different implementations, we've decoupled our Documents. Swapping things out is a breeze and we've future proofed our application a bit. If a new data source becomes available, we can implement a DataSource for the new data source and swap that in without touching our Document class. Our MySQLDataSource could use the getTable() method to determine where to save/delete the document while our RedisDataSource could use the document's class name. The document isn't concerned with how the DataSource operates, just that it does.

Make sense?

These are difficult concepts to grasp. I hope I've illustrated their usefullness clearly and in a way you can apply to your own codebase. Abstraction is a powerful thing. Learn how to harness its flexibility.

Good luck!

Tags: PHP, Intermediate