When I worked on another project, I met an interesting opinion. They say, all the code in the project should be as open as possible by default. I.e. we should make all new methods and fields public
, and we should not put final
anywhere, except some special cases when it's really necessary. It was motivated with the difficulties of further codebase support:
If I want to inherit the class, and there is a private method, I have to study the code to understand why it was made
private
and whether I can make itpublic
.
This opinion was very unusual for me, and I decided to figure it out if there is really more problems when fields and methods are private by default.
Let's write an extra public method and see how easy is it to support it.
interface FooInterface {
public function foo();
}
class Foo implements FooInterface {
public function foo() {
// do stuff
$this->bar();
// do stuff
}
public function bar() {
// do stuff
}
}