What exaclty is psr-4?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #2034
    BracketLess31
    Participant

    I was learning about composer autoloading and it used the psr-4 for autoloading classes, from what I have looked at online, is it just a naming convention for classes? I looked at php-fig but didn’t quite understand what it is, can someone explain what it is in simpler terms. Thanks!

    #2035
    Goon3r___
    Guest

    Its exactly that. PSR-4 dictates the naming convention but also dictates how that naming convention translates to a filepath.

    With a standardised way of translating the naming convention to a file path, we have a standardised way to build PSR-4 class autoloading, which is simply class autoloading that understands that naming convention.

    PSR-4 is not class autoloading itself, its simply the ‘translation’ or ‘scheme’. There are other autoloading naming standards, namely PSR-0.

    You could ofcourse roll your own, for example load all class in a single directory, and just putting all your files in there. Look at `spl_autoload_register` to see how you could build your own.

    But to summarise, PSR-4 is the naming convention and also how we turn that name into a file path.

    #2036
    colinodell
    Guest

    Historically, if you wanted to use a class you’d have to manually `require_once` the file it lives in, meaning you’d have to know the exact path. Classes are typically organized into namespaces and directories to keep them organized. If you follow a standard naming convention where the structure and names of the namespaces and directories are “the same” you can easily figure out (via PHP code) where a certain class lives and thus automatically load (or “autoload”) that file!

    Take Symfony’s `Request` class as an example. It exists as a file at [`src/Symfony/Component/HttpFoundation/Request.php`](https://github.com/symfony/symfony/blob/5.x/src/Symfony/Component/HttpFoundation/Request.php) and lives in the `SymfonyComponentHttpFoundation` namespace. See how those two things look very similar? The only difference (besides the type of slash) is that the the namespace starts with `SymfonyComponent` but the directory starts with `src/Symfony/Component`. So we need a way to specify that this partial namespace maps to a certain folder.

    This is what PSR-4 allows us to do. In fact, if you look at [Symfony’s `composer.json` file](https://github.com/symfony/symfony/blob/5.x/composer.json#L156), you can see that mapping defined:

    “autoload”: {
    “psr-4”: {
    “Symfony\Component\”: “src/Symfony/Component/”
    },
    }

    By knowing this mapping, you could write a simple function to transform the namespace name to the file path and then `require_once` that file.

    Composer fully automates that process by creating a `vendor/autoload.php` file that essentially reads those mappings, transforms the full class names with namespaces to filenames, and loads them in for you.

    #2037
    ggd_x
    Guest

    [From the docs](https://www.php-fig.org/psr/psr-4/): *This PSR describes a specification for* [*autoloading*](http://php.net/autoload) *classes from file paths.*

    Basically it is a recommended syntax for namespacing. Composer PSR-4 generates an autoloader set of files to pull in your files from described directories using this standardised notation.

    #2038
    philsturgeon
    Guest

    There’s a meta document for most PSRs which explain the motivations and use cases.

    https://www.php-fig.org/psr/psr-4/meta/

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.