2

Reading resources about object oriented programming the logic is to avoid public methods as much as possible. The public methods will provide the interface through which other objects will interact with our class.

This interface should be as simple as possible and the details of the implementation should be hidden inside private methods.

My question arises because of the non testable character of private methods.

The code should be testable right? I am not sure how these two approaches are integrated in a consistent development approach...

orestiss
  • 2,183
  • 2
  • 19
  • 23
  • Possible duplicate of [PHP: Public, Private, Protected](http://stackoverflow.com/questions/4361553/php-public-private-protected) – Sandeep Sukhija Jun 16 '16 at 07:56
  • I'm not a big expert on testing, but I don't think that you will have a problem of testing private and/or protected methods. http://stackoverflow.com/questions/249664/best-practices-to-test-protected-methods-with-phpunit – Vuk Stanković Jun 16 '16 at 08:04

2 Answers2

1

A brief description of public, private and protected:

  • public: Accessible by other classes
  • private: Not accessible by other classes
  • protected: Not accessible by other classes but accessible by derived classes

Reading resources about object oriented programming the logic is to avoid public methods as much as possible.

That's not entirely true. If a method is only used within your class and it's not part of the interface exposed by your class then of course that method needs to be private (or protected if you intend to extend your class). Public methods are part of the interface, they are methods that are designed to be called by other classes. You should not force yourself to limit the use of public methods. If a class needs a lot of public methods then it should have them. Just don't expose the inner mechanics of a class to the outside world and you will be fine.

About testing, private and protected methods can be tested with the use of Reflection. Reflection makes it possible to change the accessor of a method from private to public for testing purposes and much more. Read more about reflection here

dimlucas
  • 5,040
  • 7
  • 37
  • 54
1

You should be able to test a class by testing its public methods. If that is not enough coverage and there is still lots of functionality in private or protected methods then maybe they should be in a separate class.

Some people say that if you use TTD then all methods should start public and that private methods should only be re-factored from already working and tested code.

You can also test private or protected if you have to using mocks.

Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45
  • it is still confusing to me ... so I move my private methods to another class that will have them as public, what was the meaning of having them as private in the first place? – orestiss Jun 16 '16 at 08:12
  • @orestiss The inner mechanics of the class can remain private. This is the logic that makes your class "work" but is not relevant to the outside of the object. If you are looking at a private method which you think should be tested then its potentially time to look at the cohesion, SRP etc of the class & dictate if the logic should be exctracted into another object. If you have time, take a look at this book http://artofunittesting.com/ - it should push you in the right direction. Don't worry about the code examples in .NET you will still understand the concepts. – jakehallas Jun 16 '16 at 18:04