Default value of member variables

2020-03-12 08:06发布

What is the default value of class member variables in PHP?

Why do I often see:

public static $variable = null;

Wouldn't it be enough:

public static $variable;

标签: php oop
2条回答
趁早两清
2楼-- · 2020-03-12 08:25

It would, but some people either don't know that, or prefer to be explicit.

A common convention is to initialize to null when the programmer relies on that null value and do not initialize to null if that member is written to before being read.

查看更多
The star\"
3楼-- · 2020-03-12 08:25

Setting it to null explicitly makes the initial value clear even to people who don't know every intimate detail of PHP.

查看更多
登录 后发表回答