“公共静态” 还是“静态公共”?

发布于 2024-07-17 17:18:13 字数 644 浏览 9 评论 0原文

关于 PHP 中函数声明关键字的一个小要点:如果您有一个静态的类方法,static 关键字应该位于可见性关键字之前还是之后(public、<代码>受保护,<代码>私有)? 假设您的所有方法(静态或其他方法)都有可见性关键字,那么您希望可见性关键字相对于 function 关键字保留在同一位置:

public function foo() {}

public function bar() {}

protected function baz() {}

private function quux() {}

现在假设有几个方法是静态的:

public function foo() {}

static public function bar() {}

protected function baz() {}

static private function quux() {}

此外,如果一个方法是静态的,您希望它成为首先看到的东西,因为它对方法类型的影响甚至比可见性关键字的影响更大。

这严格来说是一个可读性问题,因为它显然没有功能或设计后果。 (我能想到的。)

A minor point about function declaration keywords in PHP: If you've got a class method that's static, should the static keyword come before or after the visibility keyword (public, protected, private)? Assuming all your methods, static or otherwise, have a visibility keyword, then you'd want the visibility keyword to remain in the same place relative to the function keyword:

public function foo() {}

public function bar() {}

protected function baz() {}

private function quux() {}

Now pretend a couple are static:

public function foo() {}

static public function bar() {}

protected function baz() {}

static private function quux() {}

Also, if a method is static, you want that to be the first thing seen, because that has more of an impact on what kind of method it is than even the visibility keyword does.

This is strictly a readability issue, as it obviously has no functional or design consequences. (That I can think of.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

怪我太投入 2024-07-24 17:18:14

你是对的,它对代码没有影响。 因此,具体做什么取决于你自己或你的团队的风格要求。 与他们协商并就风格达成一致。

如果您只是为自己编码,那么您应该为自己选择。 选择并不重要,重要的是一致性。

您可能会问的另一个问题是:您是否应该使用“public”? 为了向后兼容(PHP4 没有信息隐藏),任何没有可见性修饰符的东西默认都是公共的。 如果是公开的,您是否应该费心去写公开内容? 再次强调个人选择:无论哪种方式,都提出强有力的论据,你会让我相信你的选择是最好的。

就我个人而言,当我检查并清理自己的代码时,我喜欢将可见性修饰符放在第一位,并指定它,即使它是公共的。

You are correct in that it has no effect on the code. Therefore it is up to your own style requirements, or those of your team, as to what you do. Consult with them and agree on a style.

If you are coding for yourself only, then you should choose for yourself. The choice is not important, but consistency is.

Another question you may ask is: should you use 'public' or not? For backwards compatibility (PHP4 had no information hiding) anything without a visibility modifier is public by default. Should you bother writing public if it's public? Again personal choice: make a strong argument either way and you'll convince me your choice is best.

Personally, when I go through and clean up my own code, I like to put the visibility modifier first, and specify it even if it's public.

望笑 2024-07-24 17:18:13

来自 PSR-2:

必须在所有属性和方法上声明可见性; 抽象的
并且final必须在可见性之前声明; 静态必须是
可见性后声明。 [参考]

...如果您关心 PHP 框架互操作组标准和约定。

所以根据他们的说法,public static不是static public

From PSR-2:

Visibility MUST be declared on all properties and methods; abstract
and final MUST be declared before the visibility; static MUST be
declared after the visibility. [reference]

...if you are one to care about the PHP Framework Interop Group standard and conventions.

So public static not static public according to them.

梦醒时光 2024-07-24 17:18:13

像 Java 和 C# 这样的语言要求访问修饰符放在第一位,因此 编辑: 前面删除的行完全是错误的。 这两种语言都没有这个要求。


public static

对我来说看起来是正确的。 这两种方法都可以进行争论,我的观点是:由于“静态”限定了函数而不是访问修饰符,因此说“

<access_modifier> static

如果以其他方式使用它”则更有意义,“静态”的含义就不那么清晰了。

Languages like Java and C# require that the access modifier come first so Edit: The previous struck line is completely false. Neither language has this requirement.


public static

looks correct to me. Arguments can be made for both approaches and mine is this: Since "static" qualifies the function rather than the access modifier it makes more sense to say

<access_modifier> static

If you use it the other way around the meaning of "static" is less clear.

朦胧时间 2024-07-24 17:18:13

进一步Alexei Tenitski 的回答

I prefer static public since this way 
it is easier to spot [usually rare] static methods in classes.

所有方法都应该指定其可见性。 所以,我们知道每种方法都会在定义中的某个地方提到,唯一的问题是“它是哪个设置?”。

只有一些是静态的 - 因此,对于每一个我们都必须问“定义中的某处是否提到了 static 关键字?”。 因此,将静态放在第一位以使该问题的答案更加明显。

或者,作为一个更广泛的规则,……我倾向于把“最非凡的方面放在第一位”,这样我在阅读时就不会下意识地跳过它们。 ;o)

尝试这个测试。

很快...A类中有多少个静态方法?

class A {
 public static methodA() {
  }
 protected static methodB() {
  }
 private staticlymethodC() {
  } 
}

B 类中有多少个静态方法?

class B {
 public methodA() {
  }
 static protected methodB() {
  }
 static private methodC() {
  } 
}

我认为B类更容易快速理解。

Further to Alexei Tenitski's answer.

I prefer static public since this way 
it is easier to spot [usually rare] static methods in classes.

All methods ought to have their visibility specified. So, we know that every method is going to have that mentioned somehere in the definition, the only question is "Which setting is it?".

Only some are static - so, for each one we have to ask "Is there a mention of the static keyword somewhere in the definition?". So, put static first to make the answer to that question more obvious.

Or, as a wider rule , ......... I tend to put 'the most extraordinary aspect first' so that I don't don't subsconsciously skip over things when reading them. ;o)

Try this test.

Very quickly...How many static methods are there in Class A?

class A {
 public static methodA() {
  }
 protected static methodB() {
  }
 private staticlymethodC() {
  } 
}

and how many static methods are there in Class B?

class B {
 public methodA() {
  }
 static protected methodB() {
  }
 static private methodC() {
  } 
}

I think class B is much easier to understand quickly.

撕心裂肺的伤痛 2024-07-24 17:18:13

我不认为这是一个严格的 PHP 问题,尽管它的价值不大,但我始终更喜欢将可见性修饰符放在第一位的一致性。 我发现扫描起来更容易。

I don't think that this is a strictly PHP question, and for what little it's worth, I've always preferred the consistency of placing the visibility modifier first. I find it easier to scan.

淤浪 2024-07-24 17:18:13

我更喜欢static public,因为这样可以更容易地在类中发现[通常很少见的]静态方法。

I prefer static public since this way it is easier to spot [usually rare] static methods in classes.

娇妻 2024-07-24 17:18:13

在我使用的每种具有类型修饰符的语言中,我都将可见性放​​在第一位。

I put visibility first in every language I use that has type modifiers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文