class 中 __constructor 的函数声明没有 PHP 中的关键字 public
function declaration in a class for __constructor without the keyword public in PHP
经过长时间的搜索,我没有得到更好的清晰度,在 PHP class.[=24 的 __constructor
函数中使用关键字 public
=]
- 人们默认说
__constructor
本身就是 public。所以我不必使用提及 public
.
- 如果我在PHP中
public function __constructor
中提及或不提及public
,会不会有任何变化?
- 在 PHP 中声明
__constructor
的最佳做法是什么?有或没有关键字 public
?
- 如果我不在
public function __constructor
前面提到关键字 public
,我将面临哪些不利或问题?
PSR-2 §4.3 读作:
Visibility MUST be declared on all methods.
__constructor 是 "all methods" 之一,因此规则适用。
__constructor是一种方法。方法的可见性在 PHP 文档中描述为:
Class methods may be defined as public, private, or protected. Methods
declared without any explicit visibility keyword are defined as
public.
所以不需要为 public 方法编写 'public' 可见性。
但我仍然更喜欢为方法编写明确的可见性(甚至 'public')- 代码对每个人来说都更明显。
默认为 public。始终包含它是一个好习惯,但是 PHP4 支持 类 没有访问修饰符,因此在遗留代码中不使用它们是很常见的。
因为没有人提到它。
function __constructor ()
是一个函数,当 class 生成时将 运行。检查 了解更多详情
经过长时间的搜索,我没有得到更好的清晰度,在 PHP class.[=24 的 __constructor
函数中使用关键字 public
=]
- 人们默认说
__constructor
本身就是 public。所以我不必使用提及public
. - 如果我在PHP中
public function __constructor
中提及或不提及public
,会不会有任何变化? - 在 PHP 中声明
__constructor
的最佳做法是什么?有或没有关键字public
? - 如果我不在
public function __constructor
前面提到关键字public
,我将面临哪些不利或问题?
PSR-2 §4.3 读作:
Visibility MUST be declared on all methods.
__constructor 是 "all methods" 之一,因此规则适用。
__constructor是一种方法。方法的可见性在 PHP 文档中描述为:
Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public.
所以不需要为 public 方法编写 'public' 可见性。
但我仍然更喜欢为方法编写明确的可见性(甚至 'public')- 代码对每个人来说都更明显。
默认为 public。始终包含它是一个好习惯,但是 PHP4 支持 类 没有访问修饰符,因此在遗留代码中不使用它们是很常见的。
因为没有人提到它。
function __constructor ()
是一个函数,当 class 生成时将 运行。检查 了解更多详情