Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP composer autoload not loading class

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

like image 706
ejfrancis Avatar asked Dec 14 '22 21:12

ejfrancis


1 Answers

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

like image 191
Michal Avatar answered Dec 28 '22 14:12

Michal