Protected properties prefixed with underscores

Viewed 8101

Like:

public
  $foo        = null,
  $bar        = 10;

protected

  $_stuff     = null,
  $_moreStuff = 5;

A lot of people seem to do this. Why?

Isn't this inconsistent naming (like some PHP functions are :))?

6 Answers

I know this is an old topic but just to add something, I think this is a convention that comes from the fact that getters and setters cannot have the same name as the property they get or set.

Therefore, even in languages such as C++ where private properties exist, they were often named with an underscore prefix so that the getter/setter could use the non prefixed name.

This probably led to coders in other languages that do not have private properties, such as JavaScript to use that same convention to identify "private" properties.

Related