Question

Let us say you have a class

class View implements ViewInterface, TemplateInterface {

     private $template;

     public function setTemplate($template) {
         $this->template = $template;
         return $this;
     }
}

When you are then documenting the TemplateInterface and you come to the setTemplate($template) method what do you put down as its return type?

If I put View as the return type and then I later have different classes implementing the TemplateInterface which are not of type View my API documentation would be wrong.

Do you put down a return type of View, ViewInterface, TemplateInterface, or a mixture of them?

namespace Views\Interfaces;

interface TemplateInterface {

    /* --------------------------------- */
       public function getTemplate();
    /* ---------------------------------
     * @args: void
     *
     * @return: String - The name of the template.
    */

    /* ------------------------------------------ */
       public function setTemplate($template);
    /* ------------------------------------------
     * @arg 1: (String $template) - The name of the template file.
     *
     * @return: Not sure yet!!!
    */

}
Was it helpful?

Solution

The interface should look like:

interface TemplateInterface
{
    /**
     * @return TemplateInterface
     */
    public function setTemplate($template);
}

And the view class:

class View implements ViewInterface, TemplateInterface {

     private $template;

     /**
      * @return TemplateInterface
      * or (maybe better)
      * @return View (as it implements TemplateInterface and is more specialized)
      */
     public function setTemplate($template) {
         $this->template = $template;
         return $this;
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top