Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fsdolphin's avatar

Confused About PSR – Code Style – Autoloader – PSR-0, I, 2 etc.

Hi,

First of all let me start by say that I'm really enjoying learning Laravel in general, but sometimes I just want to give up, learning all of the different Laravel/PHP concepts it is just overwhelming, I feel like things change so fast and for someone just learning it is hard to keep up. Laravel versions change so fast that is just hard to keep up, anyways at this time the main question here is about PSR.

  1. What is PSR, is it just a code style guide line, an autoloader or both?
  1. Why did Laravel moved from PSR-4 to PSR-2?

To make things worse when you go to the www.php.fig.org they list them as follow.

PSR-0 Autoloading Standard
PSR-I Basic Coding Standard
PSR-2 Coding Style Guide
PSR-3 Logger Interface
PSR-4 Improved Autoloading

Whattt??

Sorry, I hope I'm not sounding rude by any means, I really respect the community here, you guys have been helping me a lot and as I said I'm enjoying learning about this new world, it is just different then what I have seen as a hobbyist programmer (My experience: Qt, iOS Development and general HTML/CSS - No back end experience).

Thanks

0 likes
5 replies
skliche's avatar

PSR means PHP Standard Recommendation. These are standards / recommendations covering multiple things including both code style (PSR-1, PSR-2) and autoloading (PSR-0, PSR-4).

PSR-0 and PSR-4 define how a class is found/resolved within your project's directories by the autoloader so you don't have to bother with require-statements. Have a look at your composer.json file, it contains an autoload section to define the namespace among other things (e.g. by default classes with a namespace beginning with App will be found under the directory app). PSR-0 is deprecated, PSR-4 is recommended.

For the beginning just stick to the defaults and get used to the concept of namespaces. When you are comfortable with that you can try to dig deeper into these things.

1 like
fsdolphin's avatar

@skliche

If I understand this correctly in Laravel we make use of two PSR versions, a code style (PSR-2) and an autoloader (PSR-4), correct?

Does code style recommendations are tightly related to the autoloader? In other words, will the autoloader work fine if I don't follow the recommended code style?

For the beginning just stick to the defaults and get used to the concept of namespaces. When you are comfortable with that you can try to dig deeper into these things.

This is hard to do since Laravel uses this extensively and also because you hear about these things in every video you watch.

FYI - I'm familiar with namespaces.

skliche's avatar
skliche
Best Answer
Level 42

@fsdolphin I understand your pain :-)

Yes, PSR-2 and PSR-4 are used by Laravel.

You are not forced to follow PSR-2. Autoloading still works fine without abiding to PSR-2. PSR-2 is basically only about how you format your code not about how it works.

Regarding PSR-4 you should know the following. In your composer.json the following defines the namespace for your application (defaults to App) and how to resolve classes for that namespace

"psr-4": {
            "App\\": "app/"
        }

So App\Http\Controllers\Controller will resolve to a class Controller in the file app/Http/Controllers/Controller.php. It's as simple as appending path components.

Classes outside of your app directories are resolved by things like a classmap. Have a look at the files autoload_* in vendor/composer and you should get the basic idea. As long as you use composer to require other components like laravelcollective/html composer will handle this for you and update those files.

For the record, I'm trying to describe this as simple as possible and not as pinpoint accurate as possible.

1 like
pmall's avatar

If I understand this correctly in Laravel we make use of two PSR versions, a code style (PSR-2) and an autoloader (PSR-4), correct?

The different PSR are totally unrelated. PSR2 for coding style, PSR4 for autoloading, thats all.

1 like
fsdolphin's avatar

Thanks you all for your help, PSR now make sense. I still need to look into the autoloading process a little more.

Please or to participate in this conversation.