PHP classes why use public keyword? PHP classes why use public keyword? php php

PHP classes why use public keyword?


Yes, public is the default (see visibility docs).

People add it, so it is consistent with all the other methods / properties.

Furthermore, if you want to declare a property public and don't want to use public you will need to use var, which is not recommended and will likely be deprecated at some point.


As of php 5.3 (I think, its been a while), using the var keyword raises E_STRICT errors, so public has to be used to declare object vaiables. As for functions, I believe it is more of a consistency thing.


Sure it is redundant since that is the default visibility level. You should avoid them when you can. Some people add the public keyword explicitly so that code is little more readable.