Creates single file containing multiple classes, to speed up application bootstrap.

0.4.0 2016-06-26 12:09 UTC


Creates single file PHP containing multiple classes, to speed up application bootstrap.

Scrutinizer Code Quality Build Status Code Coverage Latest Stable Version Total Downloads License


Commandline script

First, create configuration file that lists all files you want to merge. You don't need to worry about class order, nor about adding required interfaces or parent classes - they will be added automatically to merged file.

Config file is simple PHP file, returning array of class names:

// config/classes-to-cache.php
return [
    // ZF2 classes
    // custom classes

Next, use dump-classes.php script to generate cached file:

php ./vendor/bin/dump-classes.php config/classes-to-cache.php data/cache/classes.php.cache

When class cache is generated, you can include it in your application entry point:

// index.php
include 'vendor/autoload.php';
include 'data/cache/classes.php.cache';

You can automate generation using composer by adding post-install and post-update hooks to composer.json file:

    "scripts": {
        "post-install-cmd": [
            "php ./vendor/bin/dump-classes.php config/classes-to-cache.php data/cache/classes.php.cache-raw",
        "post-update-cmd": [
            "php ./vendor/bin/dump-classes.php config/classes-to-cache.php data/cache/classes.php.cache-raw",


Alternatively, cached class file can be generated in your PHP script:

$dumper = new ClassDumper();
$cache = $dumper->dump([
file_put_contents('data/cache/class_cache', "<?php\n" . $cache);

Minifing merged file

ClassDumper can reduce size of emitted file by stripping all whitespace and comments.

It can be triggered from commandline by adding --strip switch:

php ./vendor/bin/dump-classes.php config/classes-to-cache.php classes.php.cache --strip

Using in PHP:

$cache = $dumper->dump([ /* ... */ ], true);

Generating config files

You can easily generate configuration file based on currently included files in your application. In order to do it, add following lines after your application bootstrap (ideally somewhere before routing starts):

$configGenerator = new \ClassDumper\ConfigGenerator;

This will save list of all included classes into config.php file. You may want to edit configuration file manually before using it in your application - see "Limitations" section below. After creating the config file, remove above lines.


Not every class can be cached using Class Dumper.

  • class dump will end up in different directory than merged classes. Classes using constants like __DIR__or __FILE__ will likely not work correctly.
  • when using with "--strip" options, all comments - including annotations - will be stripped out. This will prevent annotation parser from working, if used on cached classes.


  • throw exception when class does not exist
  • warn if class contains __DIR__ or __FILE__ constants
  • output/log statistics
  • fix __DIR__ constants