|
Posted by Jason Barnett on 01/20/05 22:17
Richard Lynch wrote:
> Jason Barnett wrote:
>
>>Think of declaration of properties and methods as a "contract". When
>>something is public it is available to all of PHP. When it is private
>>it is only usable by the class that you define it in. When it is
>>protected it is a hybrid; it is usable to the class that defined it and
>>it can be "inherited" by classes that extend that class.
>>
>>So, decide what level of access you really *need* for that property /
>>method. If a property is only supposed to be modified by class methods
>>(for example, a password string) then make it private or possibly
>>protected. If everything is public access then there is temptation to
>>do something like:
>
>
> Back in the day, I used to code in C++
>
> Nothing irked me more than some so-called programmer who would
> over-zealously make every damned thing private.
>
> I'd go and sub-class it, and want to make my extended class actually
> USEFUL and be ham-strung by his short-sightedness.
Punk. ;)
You know as well as I do that is the reason why we have protected
members. And it is also why we use PHP: because we can quickly change
the source to fit whatever are our new requirements. Actually I find
myself using public / protected members about 90% of the time...
reserving private members only for things that just don't make sense in
a subclass. Or, things that *should* be different in a subclass (for
example, DSN information on a database class).
>
> So when you ask yourself "Self, should this be public, protected, or
> private?" make sure you phrase it as:
>
> "Can I think of ANY way in the future that some other cool programmer
> would want to do fun and interesting and useful things with this?"
>
> Let the flames begin :-)
>
The roof. The roof. The roof is on fire. ;)
Isn't everything in life a trade off? As I see it using private vs.
protected vs. public is a tradeoff between flexibility and
maintainability. If a member is private and its value is somehow messed
up, well I know that it had to have been messed up somewhere in *that*
class. If it's protected then the possible error points include the
subclasses. If it's public, well then I could have goofed up anywhere
(and believe me I DO!)
Call me crazy, but didn't someone just a few weeks ago suggest you try
to LIMIT the number of places where a bug can be found? ;)
But I digress. I said it before and I'll say it again: it's a trade off.
--
Teach a man to fish...
NEW? | http://www.catb.org/~esr/faqs/smart-questions.html
STFA | http://marc.theaimsgroup.com/?l=php-general&w=2
STFM | http://www.php.net/manual/en/index.php
STFW | http://www.google.com/search?q=php
LAZY |
http://mycroft.mozdev.org/download.html?name=PHP&submitform=Find+search+plugins
Navigation:
[Reply to this message]
|