'='和' - >'的PHP代码样式对齐

问题描述 投票:-1回答:1

对齐或不对齐......

以下方法的优点和缺点。

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

  • 在向文件添加新常量时 - 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()
                                       );
    }

}
php psr-2
1个回答
1
投票

官方回答:PSR-2有关于PHP代码的样式,你可以在这里找到它: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。

© www.soinside.com 2019 - 2024. All rights reserved.