Domanda

So in the composer.json i got the following:

"autoload": {
        "psr-0" : {
            "classmap": ["src/libraries/"]
        }
    },

and i got a class with path ~\src\libraries\ClassName.php and when I try to make an instance with new ClassName(); PHP gives me an error stating that the class ClassName is not found. And I have included the Composer autoloader. It drives me crazy

È stato utile?

Soluzione

Your autoload definition is wrong.

You are defining that you use PSR-0. You define that classes that start with the prefix "classmap" (all lowercase) are located in the directory "src/libraries".

Then you try to use a class names "ClassName". This class does not start with the letters "classmap", so it won't be loaded by this PSR-0 autoloading.

If you have file, as you stated, that conforms to PSR-0, i.e. the class name is equal to the path and file name, this would be correct for your example:

"autoload": {
    "psr-0" : {
        "ClassName": ["src/libraries/"]
    }
},

Note that there is no "classmap" anymore, because that makes no sense. You use that part of the classes names that is common to all classes that are located in that directory as the prefix - here it is "ClassName" (note that the cases match exactly the cases in the original class name, even though class names are case insensitive inside PHP - but filesystems are not, and PSR-0 does not define any case juggling, it takes the names literally).

You should try to avoid using a classmap directly if you can use PSR-0 or PSR-4. You can always optimize the Composer autoloader to expand everything into a classmap - the classmap feature itself is only there to help with old legacy code that does not conform to either standard.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top