【问题标题】:PHP code style alignment for '=' and '->''=' 和 '->' 的 PHP 代码样式对齐
【发布时间】:2017-12-29 12:39:42
【问题描述】:

对齐还是不对齐...

以下方法的优缺点。

我喜欢使用第一种情况。因为我认为它是最优的,但其他人却不这么认为。

  • 向文件添加新常量时 - diff 是 1 个字符串,而不是更多
  • 更清晰,更漂亮
  • 链式调用 (-> -> -> ->) - 更清晰

你怎么看?可能有关于它的 PSR 吗?

案例风格1

class A {

    const AB = 1;
    const PAGE = 2;
    const PP_SOMETHING_VERY_LONG = 3;

    public function testLoooooooooong()
    {
        $a = 1;
        $somethingWeryLong = 2;
        $c = 3;
        $res = $a + $c;

        $b = 1;
        $n = 100;

        return 0;
    }

    public function test2($objectttttttttttttttLooooooong) 
    {
        $objectttttttttttttttLooooooong->callSooooooooomethingLooooooong()
            ->call(
                $this->testLoooooooooong(),
                $this->testLoooooooooong2(),
                $this->testLoooooooooong3()
            )
            ->call(
                $this->testLoooooooooong(),
                $this->testLoooooooooong2(),
                $this->testLoooooooooong3()
            );
    }

}

案例风格2

class A {

    const AB                     = 1;
    const PAGE                   = 2;
    const PP_SOMETHING_VERY_LONG = 3;

    public function testLoooooooooong() {
        $a                 = 1;
        $somethingWeryLong = 2;
        $c                 = 3;
        $res               = $a + $c;

        $b = 1;
        $n = 100;

        return 0;
    }

    public function test2($objectttttttttttttttLooooooong) {
        $objectttttttttttttttLooooooong->callSooooooooomethingLooooooong()
                                       ->call(
                                           $this->testLoooooooooong(),
                                           $this->testLoooooooooong2(),
                                           $this->testLoooooooooong3()
                                       )
                                       ->call(
                                           $this->testLoooooooooong(),
                                           $this->testLoooooooooong2(),
                                           $this->testLoooooooooong3()
                                       );
    }

}

【问题讨论】:

  • 我更喜欢案例 2。我经常对齐那些“=”。但不总是。但经常。关于缩进,当代码应该缩进时,我总是只使用 4 个空格来缩进代码。例如,这适用于 IF 内的内容。我不使用 TAB 缩进。不推荐。
  • 没有实际测试,示例 1 似乎与 PSR2 匹配
  • 我已经投票结束这个问题,因为这主要是一个风格问题,完全基于意见。您唯一需要做的就是使其在您的文件中保持一致

标签: php psr-2


【解决方案1】:

官方回答:有关于样式化PHP代码的PSR-2,你可以在这里找到:http://www.php-fig.org/psr/psr-2/

除了这些建议之外,您喜欢如何设置代码样式完全取决于编码人员。这只是关于约定。

在现代 IDE(IntelliJ、Netbeans、Eclipse、PHPStorm 等)中,您可以自动将代码重新格式化为您喜欢的方式(例如,使用 ALT+L 等快捷键)。

答案中固执的部分:我更喜欢案例 1 的 = 样式和 -> 样式,因为处理所有这些空格很烦人和案例 2 中的选项卡。

我更喜欢

public function testLoooooooooong() {

}

从案例 2 开始

public function testLoooooooooong() 
{

}

出于同样的原因,来自案例 1。我不想编写额外的选项卡,我认为它不会使代码更具可读性。无论如何,这完全是一个口味问题,许多人也更喜欢这里的案例 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 2013-02-23
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多