Proper Autoloading

About Class naming, folder naming, namespaces, etc

"autoload": {
    "psr-4": { "": "src/" }

This tells Composer to use the src directory to find all classes that it doesn’t already know using the “PSR-4” standard. The details of that standard are not that important right now. Basically it says: “inside this directory, all namespaces are represented by sub directories and classes are .php files.”

Autoloading is PHP’s way to automatically find classes and their corresponding files without having to require all of them manually. You can read more about it here. Autoloading is also one of the reasons why PHP code is usually organized in classes because PHP only supports autoloading for classes, not simple functions for example.

Namespaces and Autoloading — PHP Introduction — A short introduction to PHP web development

Autoloading files

If you want to autoload functions, you can use files autoloading. This can be useful to load your helper functions, but these files will be loaded on every request.

{ “autoload”: { “files”: [“src/helpers.php”] } }

Autoloading performance: Classmap vs PSR-4


Note on Classmap and PSR-4

Classmap autoloading is the fastest among autoloaders since it loads classes from the prebuilt array. But the problem with classmap is that you need to regenerate the classmap array every time you create a new class file. So in the development environment PSR-4 autoloading will be the most suited one.

PSR-4 autoloading will be slower compared to classmap because, it needs to check the filesystem before resolving a classname conclusively. But in production you want things to be as fast as possible.

For this reason, composer offers classmap generation from PSR-4. Classmap generation converts all PSR-4/PSR-0 rules into classmap rules. So autoloading will be faster. If a PSR-4 class is not found in the generated classmap, the autoloading fallback to PSR-4 rules.


require DIR . ‘/vendor/autoload.php’; and not require ‘vendor/autoload.php’;

PHP scripts run relative to the current path (result of getcwd()), not to the path of their own file. Using DIR forces the include to happen relative to their own path.

Symfony / Fairwalter

Composer thing

Optimize Class Autoloader

There are a few options to enable this:

  • Not using: Set “optimize-autoloader”: true inside the config key of composer.json
  • composer install or update with -o (–optimize-autoloader)
  • Not using: composer dump-autoload with -o / (–optimize) - see)

Problem with migrations:

Better permissions


detaljno pročitaj:

rsync is dropping permissions

rsync –archive

sudo setfacl -R -m u:www-data:rX . sudo setfacl -R -m u:www-data:rwX ./var/cache ./var/log sudo setfacl -dR -m u:www-data:rwX ./var/cache ./var/log

php bin/console doctrine:cache:clear-result php bin/console doctrine:cache:clear-query php bin/console doctrine:cache:clear-metadata

date 06. Nov 2020 | modified 17. Jan 2023
filename: PHP » Composer