The Twelve-Factor App Applied to PHP
“One codebase tracked in revision control, many deploys”
There’s really not much here that would be different in PHP versus any other language. Who isn’t using some form of revision control these days?
“Explicitly declare and isolate dependencies”
There are a few tools available to PHP developers to help manage dependencies. The PEAR package manager may help here, but has its flaws. Pyrus, the PEAR2 package manager, looks promising. There’s also Composer. I’ve heard of people using their operating system’s package manager (e.g. RPM) to deploy their PHP applications and manage dependencies. I’m not sure if there any tools in PHP to enforce dependency isolation.
“Store config in the environment”
The simplest option here is to use environment variables. Many frameworks, including Zend Framework, allow you to have environment-specific configuration such as development, test, and production. This is not recommended for twelve-factor apps as it doesn’t scale as new environments are added.
IV. Backing Services
“Treat backing services as attached resources”
There’s not really anything to say here that’s specific to PHP.
“Strictly separate build and run stages”
Phing or the more general-purpose Ant could work here. Even though it’s not written in PHP, there’s no reason why you couldn’t use Capistrano for this. I don’t think the run stage typically applies to PHP, as it happens implicitly as part of the release stage. However, there are tasks such as flushing the APC cache (if apc.stat=0) that might be considered part of the run stage.
“Execute the app as one or more stateless processes”
PHP processes are already stateless and shared-nothing. This makes PHP a great fit for twelve-factor apps. Memory space or the filesystem should be used as a short-lived, single-process cache. If you’re using an asset manager, such as Assetic, then any assets should be compiled and cached during the build stage.
VII. Port binding
“Export services via port binding”
I don’t think port binding applies to PHP applications—at least not in the way it’s meant in twelve-factor. PHP relies on a web server and uses something like FastCGI or PHP-FPM to communicate with the web server. PHP 5.4 will have its own built-in web server, but this is intended for development use only. It’s really the combination of PHP and its web server that will be bound to a port. This brings up challenges when it comes to dependency management, as the web server itself is now a dependency.
“Scale out via the process model”
I’m not sure how processes would become first-class citizens in a PHP web application. However, each individual request/response cycle is handled by its own process. In that regard, PHP already uses the process model.
“Maximize robustness with fast startup and graceful shutdown”
PHP processes are very disposable. Again, this is just part of how PHP works.
“Keep development, staging, and production as similar as possible”
Nothing specific to PHP here. Although, it’s not uncommon for PHP developers to use SQLite in development and MySQL or PostgreSQL in production. This is not recommended for a twelve-factor app—use the same software and versions of backing services in all environments.
“Treat logs as event streams”
XII. Admin processes
“Run admin/management tasks as one-off processes”
This is easy enough in PHP. If you want an interactive shell with readline history, tab completion, and quick access to documentation then check out phpsh.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)