Translations:Developers/Object oriented programming in PHP/Methods/2/en


 * public
 * Methods declared public can be used everywhere, inside or outside of their or other classes and are inherited by subclasses. They belong to the external interface of a class and you can't change their signature without breaking compatibility to existing code, except adding more optional parameters. Public methods should normally be enforced by interfaces, so you should be sure that the methods won't change for a very long time. The amount of code in public methods should be small by sourcing out related code to protected methods to ease replacing the implemented functionality.


 * protected
 * protected methods can be accessed exclusively by methods of their own class or from subclasses. They belong to the internal interface of a class and changing their signature will also break compatibility (not if only optional parameters are added). Most of the methods used internally should be declared protected if you think their signature will be stable. They are a great way to split big methods into smaller pieces and enable subclasses to overwrite only that part of the functionality.


 * private
 * All private methods can be used only in their own class and they are not inherited by subclasses. You can change them whenever you like without affecting code outside the class. Use them if you are not sure that the method is not good/generic enough to be used by subclasses or if you think you will change that method in the future.