Re: [PEPr] Comment on PHP::Debug

From: Date: Thu, 12 Apr 2007 12:12:10 +0000
Subject: Re: [PEPr] Comment on PHP::Debug
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-46204@lists.php.net to get a copy of this message
Stefan, >> - Only use private if really needed. Protected methods/variables can be >> used from inheriting classes. > > I have read this so many times now. Was there any discussion on that > or some decision? What you are stating can be a reason against > protected methods/variables too. So, why is everyone advocating > protected? First: Visibility operators have their use and should be used whenever possible. They make clear which methods are meant for users to use, and which ones are purely an implementation detail and not meant for public service. So to come to your question: If you make all your class variables and methods (that shall not be used from outside) private, you basically disallow any classes that extend your own class to make use of the inner workings of your class. Since classes extending another class often only fix some details or add functionality, it is very unwise to not let them use the workhorses of your class. Thus, functionality that already exists in the original class cannot be used in the extending class and needs to be re-implemented, just because the author did not have in mind that his class may be extended. So general advise is the following: - If the class method/variable is to be used from outside, make it public - If it is to be used from inside the class only, make it protected - If the method is mission critical and/or will stop working when only the slightest detail is changed, or it is very very very clear that the functionality can be /only/ implemented this way (and/or another way would be dangerous), make it private. -- Regards/Mit freundlichen Grüßen Christian Weiske

Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
« previous php.pear.dev (#46204) next »