Managing PHP Class Versions especially in the Domain Model Code

posted in: PHP, Standards, Symfony | 0

When we follow Domain Driven Design (DDD) we end up with a number of Plain Old PHP Objects (POPOs).

The Domain Model will change as the project matures and it is likely that we would want to version the internal API presented by these classes.

I suggest managing the versions of  a Class through name spaces.

<?php // file Main.php

require_once './vendor/autoload.php';

use thinkadoo\demo\v2\DoDo;

class Main
{
    private $dodo;

    public function __construct()
    {
        $this->dodo = new DoDo();
    }

    public function announceVersion()
    {
        return $this->dodo->getDoDoVersion();
    }

}

$run = new Main();

echo 'The Dodo Version : ' . $run->announceVersion();

See line 5 above. The “use” instruction is all thats required to switch between versions of the Class DoDo.

It effectively selects “v2” of the class “DoDo”.

├── Main.php
├── README.md
├── composer.json
├── src
│   └── thinkadoo
│       └── demo
│           ├── v1
│           │   └── DoDo.php
│           ├── v2
│           │   └── DoDo.php
│           └── v3
│               └── DoDo.php

A repo with this working example is here: https://github.com/thinkadoo/PHPClassVersioning

Leave a Reply