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 the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Mid / Sen. Software Engineer
Clearwater, FL
ShineOn
Remote PHP / Laravel Developer
Remote
SpringboardVR
Senior PHP/Laravel Developer: Your Dream Work Environment
Remote
iPhone Photography School
Senior Laravel Developer
Leidseplein, Amsterdam
Orderchamp.com
PHP Developer
Remote
X-Team
Senior Laravel Developer (Canada and India)
London, Ontario, Canada
Factory Bucket Inc.
Laravel, PHP, PostgreSQL, Neo4J Developer
Pune, India (intern in Denver, CO)
Life AI