In a lot of my PHP projects, I end up with classes that have non-public functions that I don\'t intend to extend.
Is it best to declare these as protected, or privat
My instinct is to keep them private, until you need them to be otherwise.
It has been argued (sadly I've misplaced the link) that making methods private is antisocial, in much the same way as making them 'final', in that it's fairly dictatorial about how people may use your code.
I'm not convinced, however, and agree that you should expose only what you really need to. The exception would be a library or toolkit, where you'll expect users to want to extend (in the general sense) your code in ways which you would never foresee. In which case making well-chosen methods protected can be seen as providing flex-points.
If you intend to build a class for inheritance, you must design it that way, i.e. make those methods protected that allow other developers to change the behavior of the class along the lines that you intended. Simply making all methods protected is not a very good design. Bear in mind that all protected methods become part of the public API, so if you change things later, you will break other people's code.
In general, if you're not designing for inheritance, you should prohibit it.
Personally, I find it proper to make as much private as you possibly can. I just look at each method and ask myself if I want a derived class to be able to call it. Making everything protected leaves open the door to having methods called incorrectly.
I guess it comes down to the questions "Is everything forbidden unless specifically permitted" or "Is everything permitted unless specifically forbiffffden.
One additional factor is that it's easy to make a private method protected in a future release. It's almost impossible to privatize a method once it's made protected, as you never know what other code you invalidate.
I generally use private in some very specific occasion where the method/variable is very internal and shouldn't be read or write by any other subclass (so basically almost never). The only case in mind where I use private variable is for exemple :
final public function init():void
{
if(!_initialized)
{
_init();
_initialized = true;
}
}
protected function _init():void
{
// specific code.
}
In most of the other case the methods and variable should be useful for inheritance at least as a reading function. Because most of the time when I use code made by someone else, and when this code use private things it always and with a lot of trouble :
the wanted behavior is not already implement or complete
I can't use a method because it's private
I can't change this method because it's private
if it's protected (or if I change it as protected) then I can't rework the code (aka adding behaviors in between tow others instruction, because part of this code use private methods or variables...
= at the ends you almost change all the accessor to protected to get the ability of extending the class!
Thus, if you think this class could be extends prefer protected for everything except very specific case (if you think a specific method should only be used but not changed use final protected).
If you think this class shouldn't be extended in any case : use private.