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

前端 未结 5 630
野性不改
野性不改 2020-11-28 00:34

Recently I\'ve read about namespaces and how they are beneficial. I\'m currently creating a project in Laravel and trying to move from class map autoloading to namespacing.

5条回答
  •  长情又很酷
    2020-11-28 01:16

    Here are the major differences,

    1. 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 Acme\Foo\Bar in src/Bar.php(where Bar class is).

    2. PSR-4 does not convert underscores to directory separators

    3. You would prefer using PSR-4 with namespaces

    4. PSR-0 will not work even if the class name is different from file name, like considering above example:

    • Acme\Foo\Bar ---> src/Acme/Foo/Bar.php (for Bar class) will work
    • Acme\Foo\Bar ---> src/Acme/Foo/Bar2.php (for Bar class) will not work

提交回复
热议问题