简体   繁体   English

PHP:PSR-2:为什么命名空间/使用声明后的空白行?

[英]PHP: PSR-2: Why blank lines after namespace / use declarations?

So I was going over the PSR coding standards the other day (which I duly follow), and Ive kind of always understood the reasons for everything, and mostly coded that way for everything Ive worked on over the last 10+ years as very similar standards were heavily forced upon me in my university days - it just stuck. 所以我前几天正在审查PSR编码标准(我正式遵循),并且我总是理解所有事情的原因,并且大部分都是按照过去10年以来我所做的一切为非常相似的标准编写的在我的大学时代,我被强烈地逼迫了 - 它只是卡住了。

However, I do wonder about this note in the PSR-2 standards: 但是,我对PSR-2标准中的这个注释感到疑惑:

There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations. 在命名空间声明之后必须有一个空行,并且在使用声明块之后必须有一个空行。

Why is this? 为什么是这样? I dont object to it, but Im sure there is some kind of background or history to explain, and it never really jumped out to me as why. 我不反对它,但我确定有某种背景或历史可以解释,它从来没有真正跳到我身边。 I guess the obvious reason is legibility, but I have a feeling like there are deeper reasons for this standard. 我想明显的原因是易读性,但我觉得这个标准有更深层次的原因。

While I'm at it - does anyone know of any good resources to explain the logic behind all of the PSR guidelines? 虽然我在这里 - 有没有人知道有什么好的资源可以解释所有PSR指南背后的逻辑? Its useful to have an official reasoning and history which led to specific standards when trying to convince others to follow them, whom may not be convinced its in their best interests. 在试图说服别人跟随他们时,有一个官方的推理和历史导致了特定的标准是有用的,他们可能不相信它符合他们的最佳利益。

Well, what looks better? 那么,什么看起来更好? This: 这个:

namespace Foo;
use Bar\Baz;
use Bar\Qux;
function test()
{
}

Or this: 或这个:

namespace Foo;

use Bar\Baz;
use Bar\Qux;

function test()
{
}

I think it speaks for itself. 我认为它说明了一切。

I think the best resource you're going to get is the PHP-FIG mailing list, specifically the archive on Google Groups. 我认为您将获得的最佳资源是PHP-FIG邮件列表,特别是Google Groups上的存档。 Here are some interesting discussions related to your question: 以下是与您的问题相关的一些有趣讨论:

My personal recollection is that the standard came about as a blending of PEAR2, PHPCS, Doctrine1 and Flow3 styles. 我个人的回忆是,标准是PEAR2,PHPCS,Doctrine1和Flow3风格的混合。 But all this was 5 years ago, and I have forgotten all about it . 但所有这些都是在5年前,我已经忘记了所有这一切

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

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