-1

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!

nullcoder
  • 103
  • 2
  • 11
  • When you run `composer update`, what's the error...? – Umair Khan Feb 18 '21 at 05:22
  • Its locally installed and I see no errors, I tried `composer dumpautoload` to re generation the autoload composer.json. – nullcoder Feb 18 '21 at 05:24
  • I think it should be "xenframe\\hello\\": "xenframe/hello/src/" – Anurat Chapanond Feb 18 '21 at 05:41
  • 1
    Oh, I was just testing your code. I have to rename `index.php` with in `src` to `App.php` which relates to class name in the file. Also used `"xenframe\\hello\\": "src/"` in `psr-4` section in `composer.json`, and it started working. – Umair Khan Feb 18 '21 at 05:50
  • What have you tried to make your code PSR4 compliant? If there is any error message given, please edit your question to contain it - I would assume that it contains a hint about what is going wrong – Nico Haase Feb 18 '21 at 08:03
  • @NicoHaase there are no errors – nullcoder Feb 18 '21 at 14:40
  • You have a problem, but everything is working? That sounds strange – Nico Haase Feb 18 '21 at 14:41
  • @NicoHaase well sinse UmairKhan pointed out that i have to match the class nmes i think i kno why hwe got no errors, its because psr-4 not found does not need to give errors cus the library was loaded anyways, if the propper class wasnt found it just skiped – nullcoder Feb 18 '21 at 14:53

1 Answers1

-1

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.

Umair Khan
  • 1,684
  • 18
  • 34