Why Use a Psr-0 or Psr-4 Autoload in Composer If Classmap Is Actually Faster

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


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.

Difference between PSR-4 and classmap autoloading?

PSR-4 standard requires of you a strict filesystem structure based on namespaces. Say you have an app in src directory with App namespace, then all sub-namespaces will mirror subdirectories and class names will be the same as file names without the .php extension.

{
"autoload": {
"psr-4": { "App\\": "src/" }
}
}

src/
Foo/
Bar.php <---- App\Foo\Bar class
Baz.php <---- App\Baz class

The autoloader then "knows" where to look for the class of a certain fully qualified name and therefore doesn't require the dump-autoload command to sniff files for classes.

Performance issues are then solved with composer dump-autoload --optimize-autoloader flag, or -o, which will generate class map a similar way the classmap autoloading does.


On the other hand, classmap autoloading does not require you to have almost any certain file or directory structure, it will recursively go through .php and .inc files in specified directories and files and sniff for classes in them.

{
"autoload": {
"classmap": ["src/", "lib/", "Something.php"]
}
}

Those classes are then added to a list (cached in a PHP file in vendor/composer directory) which is used for autoloading.

Any new class then must be added to that list by running composer dump-autoload command.

What Are the Differences Between PSR-0 and PSR-4?

They are very similar so it is not surprising that it's a bit confusing. The summary is that PSR-0 had some backwards compatibility features for PEAR-style classnames that PSR-4 dropped, as such it only supports namespaced code. On top of that PSR-4 does not force you to have the whole namespace as a directory structure, but only the part following the anchor point.

For example if you define that the Acme\Foo\ namespace is anchored in src/, with PSR-0 it means it will look for Acme\Foo\Bar in src/Acme/Foo/Bar.php while in PSR-4 it will look for it in src/Bar.php, allowing for shorter directory structures. On the other hand some prefer to have the full directory structure to clearly see what is in which namespace, so you can also say that Acme\Foo\ is in src/Acme/Foo with PSR-4 which will gives you the equivalent of the PSR-0 behavior described above.

Long story short for new projects and for most intents and purposes, you can use PSR-4 and forget all about PSR-0.

composer autoload classmap with psr-4


I want to add a psr-4 loader for the rest of the files and to be able to call the files under libs without namespaces and without "use" them' kind of like aliases in laravel.

You can't use the classes without either adding use or adding the fully qualified namespace path starting with the backslash \. This has nothing to do with the way you load these classes, but is a basic requirement of PHP itself - so there is no way around it no matter how you'd like to design your autoloading.

As was commented, adding a backslash works, but this is the required minimum:

namespace App\models;
Class User{
function get(){
return \Session::get('anything');
}
}

Custom autoloader for project vs Composer PSR-4

The solution to not force relative path depth is to use the autoloader returned by Composer like this:

$loader = require 'vendor/autoload.php';
$loader->add('', __DIR__ . '/app/');

Define custom paths for some classes when using PSR-4 composer autoload

You can use classmap autoloading:

{
"autoload": {
"psr-4": {
"XXX\\Theme\\": "includes/XXX"
},
"classmap": [
"includes/template-tags/"
]
}
}

For reference, see https://getcomposer.org/doc/04-schema.md#classmap.

composer autoloader psr-0 namespaces

You have to navigate the file location of your namespace.

"autoload": {
"psr-0": { "MyNameSpace": "./<path to your parent directory>" }
}

For example, this is my directory structure:

composer.json
source
\-Data
|-Controller
\-Repository

Then, in the composer.json file:

"autoload": {
"psr-0": { "MyNameSpace": "source/Data" }
}

Then, I can define classes in these namespaces:

/* namespace for classes in controller directory */
namespace MyNameSpace\Controller;

/* namespace for classes in repository directory */
namespace MyNameSpace\Repository;

Adding new namespace location to composer psr-4

You just need to run:

composer dump-autoload



Related Topics



Leave a reply



Submit