In PHP there does not seem to be a big disparity between private and protected methods.
I asked why I should use protected in PHP Chatroom and got this answer:
unless you have good reason not to : yes
and good reason is , if you code is closed-source library
and even then , it can be overridden with reflections
So —
If the private method could potentially be overridden with Reflections, what is the point at all in private methods?
privateandprotectedare there to make it easier to write code that does the right thing. As you’ve noticed, they’re not unbreakable — there’s nothing preventing you from bypassing them if you want to. That’s because they’re meant to protect you from accidentally doing the wrong thing, not from actively trying to.That you can bypass them with reflection, doesn’t mean you should. You can consider
privateandprotectedas kinda a “warranty void if broken” sticker; if you ignore them and muck around with stuff yourself directly, the object/class might not behave properly. It has no obligation to at that point, since you’ve taken it upon yourself to mess with its innards — and the code that does so is entirely to blame.Now, as for the question in the title…you use
protectedto declare the interface specifically for subclasses; you basically declare that you intend for them to use that stuff directly, whileprivatesays that you don’t. Whether they disregard that is up to them, but if they do, then screw ’em. You tried to warn them.