phpcomposer-phppsr-4

Composer PSR-4 autoload is not working after trying multiple methods and tutorials


I was making a composer library for functionality of my framework, but I got stuck on this problem, the problem is with autoloading with psr-4.

My Configuration

I have the following structure

├───src
├───test
└───vendor
    ├───composer
    ├───symfony
    │   ├───polyfill-ctype
    │   └───polyfill-mbstring
    │       └───Resources
    │           └───unidata
    ├───twig
    │   └───twig
    │       ├───doc
    │       │   ├───filters
    │       │   ├───functions
    │       │   ├───tags
    │       │   └───tests
    │       └───src
    │           ├───Cache
    │           ├───Error
    │           ├───Extension
    │           ├───Loader
    │           ├───Node
    │           │   └───Expression
    │           │       ├───Binary
    │           │       ├───Filter
    │           │       ├───Test
    │           │       └───Unary
    │           ├───NodeVisitor
    │           ├───Profiler
    │           │   ├───Dumper
    │           │   ├───Node
    │           │   └───NodeVisitor
    │           ├───RuntimeLoader
    │           ├───Sandbox
    │           ├───Test
    │           ├───TokenParser
    │           └───Util
    └───xenframe
        └───hello
            └───src

The last folder is my library with the hello folder in it.

inside of the root of the hello folder I have a composer.json and a src folder.

composer.json

{
    "name": "xenframe/hello",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "XENONMC",
            "email": "support@xenonmc.xyz"
        }
    ],
    "minimum-stability": "stable",
    "require": {
    
    
    },
    "autoload": {

      "psr-4": {
      
        "hello\\": "/src"
      
      }

    }

}

Inside of the src/ folder is the main file - > index.php.

index.php

<?php

namespace xenframe\hello;

class App {

function __construct() {

echo "object constructed";

}

}

echo "hello world was loaded";

Now, the usage is in my root index.php.

index.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$root = str_replace('\\', '/', __DIR__);

// setup composer autoloader
require_once $root . '/vendor/autoload.php';

// visit us as https://xenonmc.xyz

use xenframe\hello\App;

$hi = new App();

What I tried

o) used psr-4 like the following "xenframe\\hello": "src/".
o) used class paths, I don't like this one because psr-4 is the recommended standard of composer.
o) using psr-0, I didn't pick this one either as there was a lot of verbose.

Thanks!


Solution

  • While testing the given code, the problem was resolved by renaming index.php within src to App.php which relates to class name in the file. Also used "xenframe\\hello\\": "src/" in psr-4 section in composer.json.

    Also its worth mentioning that while composer was unable to autoload required class, due to PSR-4 non compliance, no error was reported by composer.

    The problems were:

    1. The filename of class was not properly compliant with PSR-4 specifications. From section 2.3.3

    The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

    1. The namespace mentioned in psr-4 section in composer.json was not matching the namespace used in class file. From Composer's PSR-4 Schema

    Under the psr-4 key you define a mapping from namespaces to paths, relative to the package root. When autoloading a class like Foo\\Bar\\Baz a namespace prefix Foo\\ pointing to a directory src/ means that the autoloader will look for a file named src/Bar/Baz.php and include it if present.