Asked  7 Months ago    Answers:  5   Viewed   53 times

I understand that you can use either a PSR standard to locate files, or tell composer a directory to scan for classes. The documentation recommends using the PSR-4 standard. There is also an option for composer to create an optimized autoloader, which basically generates a full classmap. So why should one use PSR-4 at all if the best way to load is with a classmap?

It makes sense to me to keep the directory structure, since that is a good way to organize anyway. However, it seems like the logical option would be to use PSR-4 loading on development machines, and then classmap for the production environment. That way, you don't have to rebuild your classmap every time you create a new class, but the production environment creates a complete one as a part of the deployment process without an additional call to

./composer.phar dump-autoload -o

 Answers

12

Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?

Because it's more practical.

In production, you can use a classmap (with composer dumpautoload -o) because you won't add any new class, but in dev environment it's interesting to have the flexibility provided by PSR-0 or PSR-4 (i.e. nothing to do when adding new classes).

Update: you can also use composer install -o, it's simpler.

Wednesday, March 31, 2021
 
conmen
answered 7 Months ago
68

You have to read the composer and load the classes yourself for each namespaces defined into the composer.json.

Here is how :

function loadPackage($dir)
{
    $composer = json_decode(file_get_contents("$dir/composer.json"), 1);
    $namespaces = $composer['autoload']['psr-4'];

    // Foreach namespace specified in the composer, load the given classes
    foreach ($namespaces as $namespace => $classpaths) {
        if (!is_array($classpaths)) {
            $classpaths = array($classpaths);
        }
        spl_autoload_register(function ($classname) use ($namespace, $classpaths, $dir) {
            // Check if the namespace matches the class we are looking for
            if (preg_match("#^".preg_quote($namespace)."#", $classname)) {
                // Remove the namespace from the file path since it's psr4
                $classname = str_replace($namespace, "", $classname);
                $filename = preg_replace("#\\#", "/", $classname).".php";
                foreach ($classpaths as $classpath) {
                    $fullpath = $dir."/".$classpath."/$filename";
                    if (file_exists($fullpath)) {
                        include_once $fullpath;
                    }
                }
            }
        });
    }
}

loadPackage(__DIR__."/vendor/project");

new CompanyNamePackageNameTest();

Of course, I don't know the classes you have in the PackageName. The /vendor/project is where your external library was cloned or downloaded. This is where you have the composer.json file.

Note: this works only for psr4 autoload.

EDIT : Adding support for multiple classpaths for one namespace

EDIT2 : I created a Github repo to handle this code, if anyone wants to improve it.

Wednesday, March 31, 2021
 
turson
answered 7 Months ago
46

What does PHP do when working out the connection between the use of the use statement and a class it is supposed to be for?

The use statement doesn't actually load the namespace/class into your file. It simply sets up a list of aliases to refer to the classes in that namespace.

When it encounters a class that hasn't yet been declared, it uses that list of aliases to try to fully qualify the class name (prefix replacement). If it can't find an alias for the class, it'll use the namespace of the current scope to qualify the class name.

Only when the class name is fully qualified, that php will try to autoload the class (calling the various autoloaders that might have been defined).

Are PSR-0/PSR-4 autoloaders affected in the way they work when it comes to these two code samples?

No, autoloaders are not affected in the way they work by the difference in your code samples because php will call the autoloaders exactly the same way with exactly the same parameters.

Saturday, May 29, 2021
 
SubniC
answered 5 Months ago
33

There are two ways to fix it.

  1. change composer.json to

    "Portal\Core\": "src/core/"
    
  2. Or rename the core directory to Core

https://getcomposer.org/doc/04-schema.md#psr-4

The subdirectory name MUST match the case of the sub-namespace names.

http://www.php-fig.org/psr/psr-4/

Wednesday, August 11, 2021
 
Philip Weiser
answered 3 Months ago
92

From PSR-4 specification:

All class names MUST be referenced in a case-sensitive fashion.

So you'll need to rename your modules and controllers folders to Modules and Controllers respectively.

So it becomes:

app
- ...
- Modules
-- ModuleName
--- Controllers
---- BackendController.php
...

I wouldn't recommend renaming your namespaces to lowercase names because that just breaks the consistency in your code and project structure. It will be a headache to maintain and figure out which part of your namespace needs to be capitalized which one doesn't.

Monday, August 23, 2021
 
max_
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share