php autoloader composer

How to configure your autoloader in php with composer

  • install composer curl -sS | php
  • init composer ./composer.phar init
  • add PSR-4 autoload configuration with path to your source directory
    "name": "yannick/myproject",
    "description": "config php autoloader",
    "autoload": {
        "psr-4": {
            "MyNameSpace\\Blog\\": "src/myproject/blog/",
            "MyNameSpace\\Blog\\Cache\\": "src/miniblog/blog/cache",
            "MyNameSpace\\Blog\\Kernel\\": "src/miniblog/blog/kernel",
  • generate composer autoloader ./composer.phar dump-autoload
  • include autoloader in your php file and create object
    require __DIR__ . '/vendor/autoload.php';
    // get class in file src/myproject/blog/Kernel.php
    $object = new \MyNameSpace\Blog\Kernel();