Re: Re: Getting rid of "underscore for private method names" codingstandard
| From: | Daniel O'Connor | Date: | Wed, 03 Feb 2010 11:57:53 +0000 |
| Subject: | Re: Re: Getting rid of "underscore for private method names" codingstandard | ||
| References: | 1 2 3 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-53259@lists.php.net to get a copy of this message | ||
2010/1/18 Kornel Lesiński <kornel@aardvarkmedia.co.uk>
> On 16-01-2010 at 16:05:32 Greg Beaver <greg@chiaraquartet.net> wrote:
>
> However, I think a *far* better suggestion would be to forbid the use of
>> the private keyword unless the intention is to prevent any possible
>> modification because of race conditions or other critical coordination
>> issues.
>>
>
> I strongly disagree with this (unless by "critical coordination issues" you
> mean not breaking clients' derived classes and not being stuck with bad API
> forever).
>
> Protected fields and methods are part of public API. They're not really
> protected if your classes aren't final. If you make everything "protected",
> you actually expose all internal details.
>
> I think class author should consciously make decision how class can be
> extended, which means starting with everything private/final and then
> opening up appropriate API for extensibility. Otherwise class author may be
> unpleasantly surprised that there's other people's code relying on
> "protected" implementation details.
>
<?php
/** A, the handy package from PEAR, who's source code you cannot easily
modify */
class A {
private function performSecretCalculation($a, $b) {
return $a+$b;
}
public function add($a, $b) {
return $this->performSecretCalculation($a, $b);
}
}
/** B, a class which leverages A, for your specific conditions */
class B extends A {
/**
* @todo Work out what jerk made me reimplement this method by cut and
pasting.
* when all I wanted to do was make a different implementation of
add()
*/
private function performSecretCalculation($a, $b) {
return $a+$b;
}
public function add($a, $b) {
return -1 * $this->performSecretCalculation($a, $b);
}
}
$a = new A();
print $a->add(1, 2);
$b = new B();
print $b->add(1, 2);