Drop ‘public’ not ‘var’


March 26th, 2016

A PHP RFC vote has started to deprecate the var keyword in PHP 7.1 and remove it in PHP 8.

Evert Pot argues that you should be using var not public:

I’d like to offer a different opinion: I think people should be using var instead of public. I realize that this is as controversial as tabs vs. spaces (as in: it doesn’t really matter but conjures heated discussions), but hear me out!

Over the last year I’ve been going through all my open source projects, and I’ve been removing all the public declarations where this was possible.

This change has generated interesting responses. A lot of people think it’s ugly. Some think I should just follow PSR-2. I also heard from a whole bunch that weren’t aware that omitting public actually has no effect on your code!

Yes, public is completely optional. public is the default, so adding it does nothing to your code.

It is an interesting idea. Coming from the PHP 4 days I like the public but I have no real reason for why I like it. I have to assume it’s because it feels like it gives a formal structure vs the old way of marking methods as private with an underscore prefix.

I do think this is a good discussion to have. If public is implied, why do we keep using it?

Filed in:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Laravel News Partners