1

I've read that methods and member variables are public by default in PHP, yet many of the code examples I look at have the public keyword in front. For example:

class SomeClass {
    public $data;

    public function someFunction() {

    }
}

Is there any reason to use the public keyword before method and member variable names in PHP, or is clarity the only reason some people do it?

Nate
  • 26,164
  • 34
  • 130
  • 214
  • 5
    Clarity should never be underestimated in its importance – John Conde Jun 13 '14 at 19:33
  • 1
    Just in case they change the default in a future release... – developerwjk Jun 13 '14 at 19:34
  • 1
    Related: [PHP classes why use public keyword?](http://stackoverflow.com/questions/4677619/php-classes-why-use-public-keyword) – Amal Murali Jun 13 '14 at 19:35
  • 1
    If you attempt to [comply with PSR-2](http://www.php-fig.org/psr/psr-2/) _Visibility MUST be declared on all properties and methods; abstract and final MUST be declared before the visibility; static MUST be declared after the visibility._ – Michael Berkowski Jun 13 '14 at 19:36

3 Answers3

6

Is there any reason to use the public keyword before method and member variable names in PHP, or is clarity the only reason some people do it?

The reason is clarity.

Humans are coding this stuff. Humans need to read this stuff. And if a human cannot quickly decipher what is happening, lack of clarity can cause more issues.

It’s part of the reason that we also indent & properly format code. The language interpreter will eat up any code as long as the syntax is correct. Anything else—including comments—are placed there so us pieces of meat in front of a keyboard can actually make some sense of what is happening.

Using your pseudo-code example:

class SomeClass {
    public $data;

    public function someFunction() {

    }
}

Look at how easy it is to read that and understand how it works! In comparison, look at this:

class cName { $d; function fName() {} }

Technically speaking both pieces of code should work the same. But what is $d? And what is cName? And what is fName? The code would work in both cases, but who wants to spend any time untangling the second example. I mean, it works, right?

Clarity is the real key to good coding & development. This world programming & computer work is invisible—and sometimes—obtuse enough as-is. No reason to obscure how something works by just ignoring the fact that humans—not machines—create code.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
4

There is no functional difference between specifying "public" and not. There is no benefit to the code, and neither is there any sort of degradation because of it.

Just remember that your code is being written for two entities: The compiler, and for the humans who have to maintain the code. That clarity is huge.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
2

My personal opinion is that it is better to be explicit. No ambiguity. I also think that all member variables should be either protected or private with the appropriate getters/setters.

It also aids in the documentation of your code - such as DOxygen

Ed Heal
  • 59,252
  • 17
  • 87
  • 127