php PHP5。将数组声明为类成员的两种方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4557673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP5. Two ways of declaring an array as a class member
提问by ezpresso
When declaring an array as a class member, which way should it be done?
将数组声明为类成员时,应该怎么做?
class Test1 {
private $paths = array();
public function __construct() {
// some code here
}
}
or
或者
class Test2 {
private $paths;
public function __construct() {
$this->paths = array();
// some code here
}
}
Which one is better in terms of good practices and performance? What would you recommend?
哪一个在良好实践和性能方面更好?你会推荐什么?
采纳答案by Mchl
I'd suggest doing this when declaring a class variable. A constructor can be overriden in extending classes, which might result in E_NOTICEs or even E_WARNINGs if any of your functions depend on this variable being an array (even an empty one)
我建议在声明类变量时这样做。一个构造函数可以在扩展类中被覆盖,如果你的任何函数依赖于这个变量是一个数组(甚至是一个空的),这可能会导致 E_NOTICEs 甚至 E_WARNINGs
回答by sberry
In general, because I write mostly in other languages besides PHP, I like to declare my instance variables outside of the constructor. This let's me look at the top of a class and get an idea for all properties and their access modifiers without having to read the code.
一般来说,因为我主要用 PHP 之外的其他语言编写,所以我喜欢在构造函数之外声明我的实例变量。这让我可以查看类的顶部,并了解所有属性及其访问修饰符,而无需阅读代码。
For example, I really don't like methods like this
例如,我真的不喜欢这样的方法
// ...
// whole bunch of code
// ...
public function initialize() {
$this->foo = array();
// some other code to add some stuff to foo
}
Now, if I just look at the class, I can't be sure there is a variable foo even available. If there is, I don't know if I have access to it from anywhere outside the instance.
现在,如果我只看这个类,我无法确定是否有变量 foo 可用。如果有,我不知道我是否可以从实例之外的任何地方访问它。
If instead I have:
如果我有:
public $foo = array();
at the top of my class, I know that foo is an instance property, and that I can access it from elsewhere.
在班级的顶部,我知道 foo 是一个实例属性,我可以从其他地方访问它。
回答by BoltClock
If you are going to populate your array dynamically during initialization, do it in the constructor. If it contains fixed values, do it in the property declaration.
如果要在初始化期间动态填充数组,请在构造函数中进行。如果它包含固定值,请在属性声明中进行。
Trying to populate an array dynamically (e.g. by using the return value of a certain function or method) within the declaration results in a parse error:
尝试在声明中动态填充数组(例如,通过使用某个函数或方法的返回值)会导致解析错误:
// Function call is not valid here
private $paths = get_paths();
Performance is not a real concern here as each has its own use case.
性能在这里不是真正的问题,因为每个都有自己的用例。
回答by Alex Weinstein
There are no performance implications. Stop obsessing over things that don't matter - concentrate on performance problems that ARE there: measure first, optimize only the top offenders.
没有性能影响。不要再执着于无关紧要的事情——专注于存在的性能问题:首先进行测量,只优化最严重的问题。