简体   繁体   English

PSR-0和PSR-4之间有什么区别?

[英]What Are the Differences Between PSR-0 and PSR-4?

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. 我目前正在Laravel中创建一个项目,并试图从类地图自动加载转移到命名空间。 However, I can't seem to grasp what the actual difference is between PSR-0 and PSR-4. 但是,我似乎无法掌握PSR-0和PSR-4之间的实际差异。

Some resources that I've read are... 我读过的一些资源是......

What I understand: 我的理解:

  • PSR-4 does not convert underscores to directory separators PSR-4不会将下划线转换为目录分隔符
  • Certain specific rules of composer cause the directory structure to become complex which in turn makes PSR-0 namespacing verbose and thus PSR-4 was created 作曲家的某些特定规则会导致目录结构变得复杂,从而使PSR-0命名空间变得冗长,从而创建了PSR-4

Examples explaining the difference would be appreciated. 可以理解解释差异的例子。

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. 总结是PSR-0具有PSR-4丢弃的PEAR样式类名的一些向后兼容性功能,因此它只支持命名空间代码。 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. 最重要的是,PSR-4不会强制您将整个命名空间作为目录结构,而只是使用锚点之后的部分。

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. 例如,如果您定义Acme\\Foo\\名称空间锚定在src/ ,使用PSR-0意味着它将在src/Acme/Foo/Bar.php中查找Acme\\Foo\\Bar ,而在PSR-4中它将在src/Bar.php查找它,允许更短的目录结构。 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. 另一方面,有些人更喜欢使用完整的目录结构来清楚地看到哪个命名空间,所以你也可以说Acme\\Foo\\src/Acme/Foo有PSR-4,这将给你相当于上面描述的PSR-0行为。

Long story short for new projects and for most intents and purposes, you can use PSR-4 and forget all about PSR-0. 对于新项目以及大多数意图和目的而言,您可以使用PSR-4而忘记PSR-0的所有内容。

Here are the major differences, 这是主要的区别,

1. For example if you define that the Acme\\Foo\\ namespace is anchored in src/ , 1.例如,如果您定义Acme\\Foo\\命名空间锚定在src/

  • with PSR-0 it means it will look for Acme\\Foo\\Bar in src/Acme/Foo/Bar.php 使用PSR-0意味着它将在src/Acme/Foo/Bar.php查找Acme\\Foo\\Bar
  • while in PSR-4 it will look for Acme\\Foo\\Bar in src/Bar.php(where Bar class is) . 而在PSR-4中,它将在src/Bar.php(where Bar class is)查找Acme\\Foo\\Bar

2. PSR-4 does not convert underscores to directory separators 2. PSR-4不会将下划线转换为目录分隔符

3. You would prefer using PSR-4 with namespaces 3.您更希望将PSR-4与命名空间一起使用

4. PSR-0 will not work even if the class name is different from file name, like considering above example: 4.即使类名与文件名不同,PSR-0也不起作用,如上面的例子:

  • Acme\\Foo\\Bar ---> src/Acme/Foo/Bar.php (for Bar class) will work Acme\\Foo\\Bar ---> src/Acme/Foo/Bar.php (适用于Bar类)可以使用
  • Acme\\Foo\\Bar ---> src/Acme/Foo/Bar2.php (for Bar class) will not work Acme\\Foo\\Bar ---> src/Acme/Foo/Bar2.php (适用于Bar类)不起作用

PSR-4 is something like 'relative path', PSR-0, 'absolute path'. PSR-4就像'相对路径',PSR-0,'绝对路径'。

eg 例如

config: 配置:

'App\Controller' => 'dir/'

PSR-0 autoload: PSR-0自动加载:

App\Controller\IndexController --> dir/App/Controller/IndexController.php

PSR-4 autoload: PSR-4自动加载:

App\Controller\IndexController --> dir/IndexController.php

And there are some more difference in details between PSR-0 and PSR-4, see here: http://www.php-fig.org/psr/psr-4/ PSR-0和PSR-4之间的细节有所不同,请参见此处: http//www.php-fig.org/psr/psr-4/

Namespace/ folder convention. 命名空间/文件夹约定。

Classes should be stored in folders according to their namespaces. 应根据命名空间将类存储在文件夹中。

In general, you will create an src/ directory in your root folder, sitting at the same level as vendor/, and add your projects there. 通常,您将在根文件夹中创建一个src /目录,与vendor /位于同一级别,并在那里添加项目。 Below is an example of the folder structure: 下面是文件夹结构的示例:

.
+-- src
    |
    +-- Book 
    |   +-- History
    |   |   +-- UnitedStates.php - namespace Book\History;
    +-- Vehicle
    |   +-- Air
    |   |   +-- Wings
    |   |   |   +-- Airplane.php - namespace Vehicle\Air\Wings;
    |   +-- Road
    |   |   +-- Car.php - namespace Vehicle\Road;
+-- tests
    +-- test.php
+-- vendor

Difference between psr-0 and psr-4 psr-0和psr-4之间的区别

psr-0 PSR-0

It is deprecated. 它已被弃用。 Looking at vendor/composer/autoload_namespaces.php file you can see the namespaces and the directories that they are mapped to. 查看vendor/composer/autoload_namespaces.php文件,您可以看到名称空间及其映射到的目录。

composer.json composer.json

"autoload": {
        "psr-0": {
            "Book\\": "src/",
            "Vehicle\\": "src/"
        }
} 
  • Looking for Book \\History\\UnitedStates in src/Book /History/UnitedStates.php src / Book /History/UnitedStates.php中寻找Book \\ History \\ Unitedstates
  • Looking for Vehicle \\Air\\Wings\\Airplane in src/Vehicle /Air/Wings/Airplane.php src / Vehicle /Air/Wings/Airplane.php中寻找车辆 \\空气\\翅膀\\飞机

psr-4 PSR-4

Looking at vendor/composer/autoload_psr4.php file you can see the namespaces and the directories that they are mapped to. 查看vendor/composer/autoload_psr4.php文件,您可以看到名称空间及其映射到的目录。

composer.json composer.json

"autoload": {
    "psr-4": {
        "Book\\": "src/",
        "Vehicle\\": "src/"
    }
}   
  • Looking for Book \\History\\UnitedStates in src /History/UnitedStates.php src /History/UnitedStates.php中寻找Book \\ History \\ UnitedStates
  • Looking for Vehicle \\Air\\Wings\\Airplane in src /Air/Wings/Airplane.php src /Air/Wings/Airplane.php中寻找车辆 \\空气\\翅膀\\飞机

composer.json composer.json

"autoload": {
    "psr-4": {
        "Book\\": "src/Book/",
        "Vehicle\\": "src/Vehicle/"
    }
}    
  • Looking for Book \\History\\UnitedStates src/Book /History/UnitedStates.php 寻找书籍 \\历史\\美国src / Book /Hrory/UnitedStates.php
  • Looking for Vehicle \\Air\\Wings\\Airplane in src/Vehicle /Air/Wings/Airplane.php src / Vehicle /Air/Wings/Airplane.php中寻找车辆 \\空气\\翅膀\\飞机

Even when I tried but Composer is a mess. 即使我尝试但是Composer是一团糟。 Sadly, it's the only alternative.of the market. 可悲的是,它是市场的唯一选择。
Why is a mess?. 为什么乱七八糟?
The Composer's autocomplete works fine if you are in control of the code. 如果您控制代码,则Composer的自动完成功能正常。 However, if you are importing a different project, you find yourself with lots of styles and ways to create folders. 但是,如果要导入其他项目,则会发现自己有很多样式和方法来创建文件夹。 For example, some projects are /company/src/class.php while others are company/class.php and others are company/src/class/class.php 例如,一些项目是/company/src/class.php,而其他项目是company / class.php,其他项目是company / src / class / class.php

I created a library that solves it: 我创建了一个解决它的库:

https://github.com/EFTEC/AutoLoadOne (it's free, MIT). https://github.com/EFTEC/AutoLoadOne (它是免费的,麻省理工学院)。

It generates an autoinclude by scanning all the classes of a folder, so it works in every case (psr-0 psr-4, classes without namespace, file with multiple classes.. 它通过扫描文件夹的所有类来生成自动包含,因此它适用于所有情况(psr-0 psr-4,没有命名空间的类,具有多个类的文件..

edit: And again, downvoted without any reason. 编辑:再次,毫无理由地投票。 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM