3

I am new to using composer and have created a list of required projects in my composer.json file. I can use them within my index.php bootstrap file fine.

However I want to also be able to autoload my own project files outside of the vendor folder using composers autoloader. My folder structure is as follows:

vendor/
project/
   Project.php
index.php
composer.json

index.php

<?php

require 'vendor/autoload.php';

new \Project\Project;

Project.php

<?php

namespace Project;

class Project {
}

composer.json

{
    "autoload": {
        "psr-4": {
            "Project\\": "project"
        }
    }
}

This keeps coming up with the following error:

Fatal error: Class 'Product\Product' not found in index.php on line 5

What am i doing wrong? Or can I not use composers autoload to load my application files?

Edit Turns out I needed to run composer dump-auto -o to refresh the changes I made to the composer.json file. Credit to @Quasduck who posted in the comments.

Ozzy
  • 10,285
  • 26
  • 94
  • 138
  • There seems to be a disconnect between the namespace you are declaring and the one you are using. Are you sure the class you're trying to instantiate shouldn't be `Project\Product\Product` ? – Jeff Lambert Jul 21 '14 at 16:22
  • It should be `new \Project\Project;` - without the trailing `.php`. And also make sure you don't forget to run `composer dump-auto -o` after making changes in the autoload section of your composer.json - And where does that `Product` thing come from in your error message? Is there a Product class involved anywhere? – Quasdunk Jul 21 '14 at 16:23
  • @Quasdunk The .php was a typo on SO and isnt in the code itself. The problem was fixed when I run the `composer dump-auto -o` which you suggested and the class was loaded fine :) – Ozzy Jul 21 '14 at 16:41
  • Alright, glad it works now :) Good luck with your project! – Quasdunk Jul 21 '14 at 16:42
  • @Quasdunk please post your solution as an answer, so Ozzy can mark the question as solved. – Wouter J Jul 22 '14 at 12:08

1 Answers1

3

Posting my comment as an answer

Whenever you tinker with the autoload section in your composer.json file, always make sure to update the autoloader after that by running

$ composer dump-autoload -o

This is also run automatically after each composer update or composer install.

Also notice the (optional, but recommended) -o param, which tells Composer to optimize the autoloads. This basically means that PSR-0/4 autoloads (like in your example) are converted to simple classmaps. This can speed up the autoloading significantly, especially in larger projects.

Updating the autloader can also solve issues when you update, rename or move a class but your application doesn't seem to pick up on that.

Quasdunk
  • 14,944
  • 3
  • 36
  • 45
  • I want to question the remark regarding `-o`. I gave some reasons why this is not true in general in http://stackoverflow.com/questions/22803419/why-use-a-psr-0-or-psr-4-autoload-in-composer-if-classmap-is-actually-faster/22823995#22823995 – Sven Jul 22 '14 at 17:17
  • @Sven Thanks for the hint, I didn't actually think of that. Very interesting issue though! – Quasdunk Jul 22 '14 at 19:05