Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Components
  4. Class Loader
  5. The Class Map Generator
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Generating a Class Map
  • Dumping the Class Map

The Class Map Generator

Edit this page

Warning: You are browsing the documentation for Symfony 3.2, which is no longer maintained.

Read the updated version of this page for Symfony 7.0 (the current stable version).

The Class Map Generator

Loading a class usually is an easy task given the PSR-0 and PSR-4 standards. Thanks to the Symfony ClassLoader component or the autoloading mechanism provided by Composer, you don't have to map your class names to actual PHP files manually. Nowadays, PHP libraries usually come with autoloading support through Composer.

But from time to time you may have to use a third-party library that comes without any autoloading support and therefore forces you to load each class manually. For example, imagine a library with the following directory structure:

1
2
3
4
5
6
7
8
9
library/
├── bar/
│   ├── baz/
│   │   └── Boo.php
│   └── Foo.php
└── foo/
    ├── bar/
    │   └── Foo.php
    └── Bar.php

These files contain the following classes:

File Class Name
library/bar/baz/Boo.php Acme\Bar\Baz
library/bar/Foo.php Acme\Bar
library/foo/bar/Foo.php Acme\Foo\Bar
library/foo/Bar.php Acme\Foo

To make your life easier, the ClassLoader component comes with a ClassMapGenerator class that makes it possible to create a map of class names to files.

Generating a Class Map

To generate the class map, simply pass the root directory of your class files to the createMap() method:

1
2
3
use Symfony\Component\ClassLoader\ClassMapGenerator;

var_dump(ClassMapGenerator::createMap(__DIR__.'/library'));

Given the files and class from the table above, you should see an output like this:

1
2
3
4
5
6
7
Array
(
    [Acme\Foo] => /var/www/library/foo/Bar.php
    [Acme\Foo\Bar] => /var/www/library/foo/bar/Foo.php
    [Acme\Bar\Baz] => /var/www/library/bar/baz/Boo.php
    [Acme\Bar] => /var/www/library/bar/Foo.php
)

Dumping the Class Map

Writing the class map to the console output is not really sufficient when it comes to autoloading. Luckily, the ClassMapGenerator provides the dump() method to save the generated class map to the filesystem:

1
2
3
use Symfony\Component\ClassLoader\ClassMapGenerator;

ClassMapGenerator::dump(__DIR__.'/library', __DIR__.'/class_map.php');

This call to dump() generates the class map and writes it to the class_map.php file in the same directory with the following contents:

1
2
3
4
5
6
<?php return array (
'Acme\\Foo' => '/var/www/library/foo/Bar.php',
'Acme\\Foo\\Bar' => '/var/www/library/foo/bar/Foo.php',
'Acme\\Bar\\Baz' => '/var/www/library/bar/baz/Boo.php',
'Acme\\Bar' => '/var/www/library/bar/Foo.php',
);

Instead of loading each file manually, you'll only have to register the generated class map with, for example, the MapClassLoader:

1
2
3
4
5
6
7
8
9
10
11
12
use Symfony\Component\ClassLoader\MapClassLoader;

$mapping = include __DIR__.'/class_map.php';
$loader = new MapClassLoader($mapping);
$loader->register();

// you can now use the classes:
use Acme\Foo;

$foo = new Foo();

// ...

Note

The example assumes that you already have autoloading working (e.g. through Composer or one of the other class loaders from the ClassLoader component.

Besides dumping the class map for one directory, you can also pass an array of directories for which to generate the class map (the result actually is the same as in the example above):

1
2
3
4
5
6
use Symfony\Component\ClassLoader\ClassMapGenerator;

ClassMapGenerator::dump(
    array(__DIR__.'/library/bar', __DIR__.'/library/foo'),
    __DIR__.'/class_map.php'
);
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Version:
    Get your Sylius expertise recognized

    Get your Sylius expertise recognized

    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Symfony footer

    Avatar of fduch, a Symfony contributor

    Thanks fduch for being a Symfony contributor

    1 commit • 51 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony