“公共静态"或“静态公众"? [英] "public static" or "static public"?

查看:175
本文介绍了“公共静态"或“静态公众"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于PHP中的函数声明关键字的一点说明:如果您有一个静态的类方法,则static关键字应该在可见性关键字(publicprotectedprivate)之前还是之后?假设您所有的方法(静态的或其他方法)都有一个可见性关键字,那么您希望该可见性关键字相对于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() {}

此外,如果某个方法是静态的,则您希望该方法是首先见到的,因为与可见性关键字相比,它对哪种方法的影响更大. /p>

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

解决方案

来自PSR-2:

必须在所有属性和方法上声明可见性;抽象的 并且必须在可见性之前声明最终结果;静态必须 在可见性之后声明. [参考]

...如果您关心的是PHP Framework Interop Group标准和约定.

因此,public static并非static public.

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.)

解决方案

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.

这篇关于“公共静态"或“静态公众"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆