简体   繁体   English

PHP变量类型的类属性

[英]PHP variable type of class property

Lets take this code as an example: 让我们以以下代码为例:

class foo
{
    private $segments = NULL;
}

$segments will be an array later. $segments将在以后成为一个数组。

The question: 问题:

Should i tell PHP that $segments is an array ( private $segments = array(); ) or leave it as NULL? 我应该告诉PHP $segments是一个数组( private $segments = array(); )还是将其保留为NULL?

What is better? 什么是更好的?

Thank you! 谢谢!

if you are sure it will be an array later, declare it as an array, if it may change during the program, just keep it as it is. 如果确定以后将要使用数组,则将其声明为数组,如果在程序执行过程中可能会更改,请保持原样。

PHP is non-typed language, so it doesn't really matter, it just a matter of standards and habits. PHP是非类型化语言,因此它并不重要,它仅取决于标准和习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM