本文介绍了PHP5。将数组声明为类成员的两种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当声明数组为类成员时,应该采用哪种方法?
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
}
}
或
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?
推荐答案
我建议在声明类变量时这样做。构造函数可以在扩展类中覆盖,如果任何函数依赖于这个变量是一个数组(甚至是一个空数组),那么可能会导致E_NOTICEs甚至E_WARNING。
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)
这篇关于PHP5。将数组声明为类成员的两种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!