Speeding up PHP application bootstrap with Class Dumper

One reasons for PHP still being considered slow is a consequence of how it
works under web server environment: every time a client sends request, application
is initialized from scratch - it runs all the bootstrap code.
Bootstrapping is repeated over and over again, for every connecting client.

While this is an obvious waste of resources, it is also very difficult to avoid
without rewriting an application under different architecture. Is there anything
that could be done to at least reduce impact of application bootstrap, without
making any changes to actual application? As it turns out, there is.

Merging class files

Even simple application written in modern, full-stack framework can use 100-200
classes just to show "Hello, World" in the browser. For each of this classes PHP
needs to check if file exists, then open it and parse. This creates significant
overhead even when opcache is enabled. There's an easy way to go around it though:
you can put all classes that used on every request in a single file.

There are at least two frameworks that can merge this files for you: Symfony
(via Bootstrap Files)
and Zend Framework (via EdpSuperluminal module).
Their solutions are difficult to reuse though, as they are tailored to work with given framework.

Class Dumper

Using ideas from this projects as a base, I crafted a general solution, that can be used everywhere:
Class Dumper.
Idea is to let you choose any classes you want, and merge them together. Just as it is done
in Symfony, you can automate this process with composer hooks - merged file will be refreshed
whenever you update your dependencies.

Example

This is how your config could look like when building Zend Expressive app:

--strip switch will ensure that generated file is as small as possible.
Finally, start using cached classes, by including generated file in your index.php:

<?php
include 'vendor/autoload.php';
include 'classes.php.cache';

Efficiency

Real performance gain can vary depending on number of files cached and your PHP configuration. I've tried
it under different conditions, and I've seen improvements ranging from 2% to 10%. Of course if your
App does some IO (database access), the end result will be even smaller. On the other hand, efficiency will
increase with number of files merged together. Always ensure that OpCache enabled, and that merged file doesn't
grow too big (try keeping it below 1MB).

Despite low efficiency in some cases, this optimization is worth considering, as it comes almost for free -
create list of classes, update composer.json, and that's it!

More resources

Class Dumper can be found on Github, where you
can also find detailed usage options, example composer hook, and limitations. Project is also
available on Packagist.