PHPackages                             classpreloader/classpreloader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. classpreloader/classpreloader

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

classpreloader/classpreloader
=============================

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

4.3.0(1y ago)37642.4M—4.6%3020MITPHPPHP ^7.2.5 || ^8.0

Since Mar 3Pushed 1y ago10 watchersCompare

[ Source](https://github.com/ClassPreloader/ClassPreloader)[ Packagist](https://packagist.org/packages/classpreloader/classpreloader)[ GitHub Sponsors](https://github.com/GrahamCampbell)[ Fund](https://tidelift.com/funding/github/packagist/classpreloader/classpreloader)[ RSS](/packages/classpreloader-classpreloader/feed)WikiDiscussions 4.3 Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (24)Used By (20)

Class Preloader for PHP
=======================

[](#class-preloader-for-php)

This tool is used to generate a single PHP script containing all of the classes required for a specific use case. Using a single compiled PHP script instead of relying on autoloading can help to improve the performance of specific use cases. For example, if your application executes the same bootstrap code on every request, then you could generate a preloader (the compiled output of this tool) to reduce the cost of autoloading the required classes over and over.

[![Banner](https://user-images.githubusercontent.com/2829600/71563674-7467c580-2a8b-11ea-8776-4bb143a03e47.png)](https://user-images.githubusercontent.com/2829600/71563674-7467c580-2a8b-11ea-8776-4bb143a03e47.png)

What it actually does
---------------------

[](#what-it-actually-does)

This tool listens for each file that is autoloaded, creates a list of files, traverses the parsed PHP file using [PHP Parser](https://github.com/nikic/PHP-Parser) and any visitors of a Config object, wraps the code of each file in a namespace block if necessary, and writes the contents of every autoloaded file (in order) to a single PHP file.

Notice
------

[](#notice)

This tool should only be used for specific use cases. There is a tradeoff between preloading classes and autoloading classes. The point at which it is no longer beneficial to generate a preloader is application specific. You'll need to perform your own benchmarks to determine if this tool will speed up your application.

Installation
------------

[](#installation)

Add ClassPreloader as a dependency to your composer.json file by adding `"classpreloader/classpreloader": "^4.3"` to your require block. Note that if you want to use the cli tool, then you need to also add `"classpreloader/console": "^3.3"` to the require block.

Using the tool
--------------

[](#using-the-tool)

You use the `./vendor/bin/classpreloader` compile command with a few command line flags to generate a preloader.

`--config`: A CSV containing a list of files to combine into a classmap, or the full path to a PHP script that returns an array of classes or a `ClassPreloader\ClassLoader\Config` object.

`--output`: The path to the file to store the compiled PHP code. If the directory does not exist, the tool will attempt to create it.

`--skip_dir_file`: (no value) Skip files with `__DIR__` or `__FILE__` to make the cache portable.

`--fix_dir`: (defaults to 1) Set to 0 to not replace `__DIR__` constants with the actual directory of the original file.

`--fix_file`: (defaults to 1) Set to 0 to not replace `__FILE__` constants with the actual location of the original file.

`--strict_types`: (defaults to 0) Set to 1 to enable strict types mode.

`--strip_comments`: (defaults to 0) Set to 1 to strip comments from each source file.

Writing a config file
---------------------

[](#writing-a-config-file)

Creating a PHP based configuration file is fairly simple. Just include the `vendor/classpreloader/classpreloader/src/ClassLoader.php` file and call the `ClassPreloader\ClassLoader::getIncludes()` method, passing a function as the only argument. This function should accept a `ClassPreloader\ClassLoader` object and register the passed in object's autoloader using `$loader->register()`. It is important to register the `ClassPreloader\ClassLoader` autoloader after all other autoloaders are registered.

An array or `ClassPreloader\ClassLoader\Config` must be returned from the config file. You can attach custom node visitors if you need to perform any sort of translation on each matching file before writing it to the output.

```
