PHP composer autoload not loading class

ejfrancis picture ejfrancis · Jun 24, 2014 · Viewed 13.3k times · Source

I'm just getting started using composer for dependency management and I'm having a hard time figuring out how I'm not adhering to psr-4 for autoloading so I'm here for advice. I've got a class that generates random values that is already on the packagist. The project structure is the following (I've labeled the composer.json files A and B):

project dir

  |classfile.php 
A |composer.json
  |vendor
   |autoload.php
    |ejfrancis
      |php-random-value
B       |composer.json
        |RandomValue.php        <--the class I want autoloaded    

composer.json A

{
    "require": {
        "ejfrancis/php-random-value": "dev-master"
    }
}

composer.json B

{
    "name": "ejfrancis/php-random-value",
    "description": "Secure random value generator.",
    "require": {
        "php": ">=5.3.0"        
    },
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "ejfrancis\\" : ""
        }
    }
}

and finally the RandomValue.php file, which declares the ejfrancis namespace

namespace ejfrancis;

class RandomValue{
  //foo
}

When I run the app I get an error 'class RandomValue not found', so it's not autoloading correctly. Am I not complying to psr-4, or is there something else I'm doing wrong? I've also tried autoloading just using a composer classmap like "classmap" : ["RandomValue.php"] to no success. Thanks for the help

Update: I've run 'composer validate' on the composer.json B file, it definitely is valid

Answer

Michal picture Michal · Jun 24, 2014

Change in your composer to "Namespace\\" and do a composer dump-autoload -o