Drop ‘public’ not ‘var’

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?

Visit Resource →

Filed in: News

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Laravel Senior Developer
Remote, Canada Only
BeMo Academic Consulting
Senior Fullstack Developer / Architect (w/m/d)
Remote / Munich - Germany
envivo.select GmbH
Medior full stack developer (Laravel)
Deventer (or remote) in the Netherlands, Dutch speaking required.
MSML B.V.
Senior Full Stack PHP Developer (Laravel)
Remote
MAPPEN
Senior Laravel Developer
Remote
ACTO