Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer with PSR-4 autoloading: classes from namespace not loading

I have the follow project structure:

- root
|- src <- Application specifc source
  |- [...]
|- tests
  |- [...]
|- Vendor
  |- myusername <- shared packages for all projects
    |- src
      |- MyNamespace
        |- File.php
  |- autoload.php
  |- test.php
|- composer.json

composer.json already have a PSR-4 entry:

"autoload": {
     "psr-4": {
         "MyNamespace\\":"myusername/src"
     }
}

/Vendor/test.php

<?php
require 'autoload.php';

$file = new MyNamespace\File();
echo $file->isDone();

Vendor/myusername/src/MyNamespace/File.php

<?php
namespace MyNamespace;

class File
{
    public function isDone()
    {
        return 'Done!';
    }
}

But I always get fatal error Fatal error: Class 'MyNamespace\File' not found in [...]

Are the composer settings or file structure correct? What I can do?

EDIT 1:

I can load external vendors fine

like image 442
Gabriel Santos Avatar asked Apr 03 '14 14:04

Gabriel Santos


People also ask

What's the purpose of PSR 4 autoloading?

Overview. This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.

How autoload PHP class the composer way?

After you create the composer. json file in your project root with the above contents, you just need to run the composer dump-autoload command to create the necessary autoloader files. These will be created under the vendor directory. Finally, you need to include the require 'vendor/autoload.

What is PSR 4 autoloading standard laravel?

The PSR-4 autoloading standard requires the fully qualified class name to match the filesystem path, and is case-sensitive. The namespace prefix is mapped by the psr-4 option in your composer. json .

What is Classmap in composer JSON?

Classmap# The classmap references are all combined, during install/update, into a single key => value array which may be found in the generated file vendor/composer/autoload_classmap. php . This map is built by scanning for classes in all .


Video Answer


1 Answers

There are 2 things wrong with your code.

You are using PSR-4 wrong.

They removed the need to embed the namespace in your folders, making a cleaner footprint in your project folder.

PSR-0
vendor/<VendorName>/<ProjectName>/src/<NamespaceVendor>/<NamespaceProject>/File.php

PSR-4 (See that they removed the namespaces folders? Because you already reference that in composer.json
vendor/<VendorName>/<ProjectName>/src/File.php

So in your case it would be:

Vendor/myusername/src/File.php

Your composer.json is invalid

         "MyNamespace\\":"myusername/src"

Doesn't include the full path to the directory with your project's code. It should be like this:

"autoload": {
     "psr-4": {
         "MyNamespace\\": "Vendor/myusername/src"
     }
}

but the best way to store your files would be outside the vendor directory, as that is used by automatically downloaded libraries, instead choose a different "development" directory:

"autoload": {
     "psr-4": {
         "MyUsername\\MyProject\\": "src/myusername/myproject/src"
     }
}

Thanks to Sven in the comments.

like image 118
Coded Monkey Avatar answered Sep 30 '22 10:09

Coded Monkey