tags:

views:

30

answers:

2

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;
+1  A: 

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.

Artefacto
I'd prefer people needlessly do this than have people come to C and leave out the "optional" initializer :)
Michael Mrozek
@Michael Well, it's optional for globals :p
Artefacto
A: 

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

Joey