Alexander Steshenko | On Software Engineering

DDD in PHP: Introduction

Domain Driven Design

You can find an article explaining what Domain Driven Design is here It’s good and simple enough. I will make some simplifications in this post, talking from point of view of a PHP developer.

To recap the wikipedia article in my own words, DDD is a way to organize a project so that it structurally reflects your domain model.

If we talk about OOP, ideally, objects from domain model, their possible states and what actions are possible upon them become objects in terms of the programming language you use described with OOP features of the language.

DDD + PHP = ?

Recently DDD has gained some popularity in the PHP community. Particularly due to the development and recent release of Doctrine 2 ORM - a tool which opens great possibilities for designing the business model freely and as you “visualize” (almost). This popularity, however, comes from two different points of view:

Neither is correct in my opinion. Modern solid and integral frameworks that affect the way you design your model (e.g. Rails for Ruby, Yii, ZF (parts of it) etc) are all created to make life of a web-site developer easier. Particularly, to simplify CRUD-like tasks. To ship all those “users/comments/blogs/news/ratings” right out-of-the-box. And that’s great! You should follow the best practices whatever those practices are and use those frameworks if you see that it suits your needs just fine.

I’d say that DDD wouldn’t be good for most of currently existing web sites. And even more, if you try to apply the methodology, not using the frameworks and practices you’re used to: you risk to join those who think DDD is just three letters serving no real purpose.

Should I use it?

How to exactly determine whether DDD is a right choice for your project is a tricky question but I have some simple guide lines:

Try DDD if:

Stick to your framework:

A few more things to keep in mind.

If you’re not sure about what you’re doing and it’s new to you - do not try to apply Domain Driven Design in a time-critical project without clear understanding of what challenges might pop up.

To successfully apply DDD approach in an application that has many layers (e.g. various front-ends, different kinds of data storages) you’re gonna need some profound tools. Doctrine 2 ORM is one of those tools. You may need to use things like DI and AOP to avoid workarounds and hacks.

You can also try to mix approaches to ease the migration from the model design you’re used to.

This post is the introduction to the series in which I’m going to show different aspects of developing a real web application using DDD building blocks and neighbouring patterns in PHP.

Comments and feedback are much appreciated. See contact details here