Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alihoushyaripour's avatar

What exactly does --optimize-autoloader command?

Hi,

I was want to publish my laravel project, because that I was read this document:

https://laravel.com/docs/5.6/deployment

It says run this artisan command for optimization:

composer install --optimize-autoloader --no-dev

I was run it but this command was removed 32 packages of my project. It showed me this:

Loading composer repositories with package information
Installing dependencies from lock file
Package operations: 0 installs, 0 updates, 32 removals
  - Removing webmozart/assert (1.3.0)
  - Removing theseer/tokenizer (1.1.0)
  - Removing symfony/thanks (v1.1.0)
  - Removing sebastian/version (2.0.1)
  - Removing sebastian/resource-operations (2.0.1)
  - Removing sebastian/recursion-context (3.0.0)
  - Removing sebastian/object-reflector (1.1.1)
  - Removing sebastian/object-enumerator (3.0.3)
  - Removing sebastian/global-state (2.0.0)
  - Removing sebastian/exporter (3.1.0)
  - Removing sebastian/environment (4.0.1)
  - Removing sebastian/diff (3.0.1)
  - Removing sebastian/comparator (3.0.2)
  - Removing sebastian/code-unit-reverse-lookup (1.0.1)
  - Removing phpunit/phpunit (7.4.5)
  - Removing phpunit/php-token-stream (3.0.1)
  - Removing phpunit/php-timer (2.0.0)
  - Removing phpunit/php-text-template (1.2.1)
  - Removing phpunit/php-file-iterator (2.0.2)
  - Removing phpunit/php-code-coverage (6.1.4)
  - Removing phpspec/prophecy (1.8.0)
  - Removing phpdocumentor/type-resolver (0.4.0)
  - Removing phpdocumentor/reflection-docblock (4.3.0)
  - Removing phpdocumentor/reflection-common (1.0.1)
  - Removing phar-io/version (2.0.1)
  - Removing phar-io/manifest (1.0.3)
  - Removing myclabs/deep-copy (1.8.1)
  - Removing mockery/mockery (1.2.0)
  - Removing hamcrest/hamcrest-php (v2.0.0)
  - Removing fzaninotto/faker (v1.8.0)
  - Removing filp/whoops (2.3.1)
  - Removing doctrine/instantiator (1.1.0)
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover

And now in several place of my code error has occurred.

Class 'SebastianBergmann\ObjectEnumerator\Enumerator' not found.
...

But why? What should I do? Please help me

0 likes
2 replies
manelgavalda's avatar
Level 50

The --optimize-autoloader flag just creates a class-map wich will speed autoloading in your application, It doesn't remove any package.

The packages are removed because the --no-dev flag. The --no-dev flag basically removes all the composer packages that are in the require-dev section of your composer.json, so it just removed the packages used in development.

If the flag removed packages that the application in production is using, you should put these packages on the require section instead.

5 likes
debiprasad's avatar

@manelgavalda I would like to add one more point.

In Laravel, "optimize-autoloader" is set true in composer.json by default. So, adding --optimize-autoloader flag not required if "optimize-autoloader": true.

The composer docs says:

due to the way PSR-4 and PSR-0 autoloading rules are set up, it needs to check the filesystem before resolving a classname conclusively. This slows things down quite a bit, but it is convenient in development environments because when you add a new class it can immediately be discovered/used without having to rebuild the autoloader configuration.

It also says:

Note: You should not enable any of these optimizations in development as they all will cause various problems when adding/removing classes. The performance gains are not worth the trouble in a development setting.

In my developer environment, "optimize-autoloader" is set true in composer.json. But I have never experienced any visible issue. This is because, if autoload misses (i.e. when it cannot find a given class), those fallback to PSR-4 rules and can still result in slow filesystem checks.

If I set "optimize-autoloader" to false, will it improve my experience in my developer environment?

Please or to participate in this conversation.