Static method and testable code

Following this discussion

http://www.linkedin.com/groupAnswers?viewQuestionAndAnswers=&discussionID=130090944&gid=42193

enlight me on use of static methods and relation with TDD, what I learned so far is that is better to define a collection class implementing a collection interface, which has method that operate on collection of object, if this is the intend of the use of static methods. Example:

Interface iEntityCollection {
   /**
    * @param int
    * @return Entity
    */
   public function loadById($id);
   /**
    * @return string
    */
   public function asHtml();
}

Class EntityCollection implements iEntityCollection {
   private $instance = null;
   private function __construct();
   public static function getInstance() {
      // the usual singleton
   }
   private $_entities = array();
   // ... implement interface method
   // ...
}

The advances is that an eventual consumer class can accept a iEntityCollection, thus having type checking via type hint, not rely on class name, and make it possible to compare performance of differrent implementations.

Discuss it in: http://www.linkedin.com/groupAnswers?viewQuestionAndAnswers=&discussionID=130090944&gid=42193


Posted

in

,

by

Tags: