Composer expected class(but not found) when trying to autoload PHP file

I am trying to autoload just a PHP file without any class in it but with helper functions instead. Something like Laravel helpers so:

<?php

if (!function_exists('fixture')) {
    function fixture()
    {
        // some code here
    }
}

According to the docs it’s quite easy, I am doing something like that:

"autoload": {
        "files": [
            "src/Helpers/helpers.php"
        ],
        "psr-4": {
            "App": "src/"
        }
    },

After that, I am doing composer dumpautoload to regenerate the whole thing. Then the strange thing happens.

If that matters I am using Symfony 5. For as long as I keep my helper file in src directory I get this error:

Fatal error: Uncaught SymfonyComponentDependencyInjectionExceptionInvalidArgumentException: Expected to find class "AppHelpershelpers" in file "/srv/app/src/Helpers/helpers.php" while importing services from resource "../src/*", but it was not found! Check the namespace prefix used with the resource. in /srv/app/vendor/symfony/dependency-injection/Loader/FileLoader.php:206

When I move it outside the src directory it seems to be working fine. It looks to me like composer decides that psr-4 is more important than the file key.

What is interesting Laravel does the same thing and it apparently works just fine. Example:

"autoload": {
        "files": [
            "src/Illuminate/Foundation/helpers.php",
            "src/Illuminate/Support/helpers.php"
        ],
        "psr-4": {
            "Illuminate": "src/Illuminate/"
        }
    },

Does anyone have any idea why this could be happening? I am sure it’s something silly, I just can’t see what it is…

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *