title: Does PHP have an answer to Java style class generics?
body: <p>Upon building an MVC framework in PHP I ran into a problem which could be solved easily using Java style generics. An abstract Controller class might look something like this:</p>
  
  <pre><code>abstract class Controller {
  
  abstract public function addModel(Model $model);
  </code></pre>
  
  <p>There may be a case where a subclass of class Controller should only accept a subclass of Model. For example ExtendedController should only accept ReOrderableModel into the addModel method because it provides a reOrder() method that ExtendedController needs to have access to:</p>
  
  <pre><code>class ExtendedController extends Controller {
  
  public function addModel(ReOrderableModel $model) {
  </code></pre>
  
  <p>In PHP the inherited method signature has to be exactly the same so the type hint cannot be changed to a different class, even if the class inherits the class type hinted in the superclass. In java I would simply do this:</p>
  
  <pre><code>abstract class Controller&lt;T&gt; {
  
  abstract public addModel(T model);
  
  
  class ExtendedController extends Controller&lt;ReOrderableModel&gt; {
  
  public addModel(ReOrderableModel model) {
  </code></pre>
  
  <p>But there is no generics support in PHP. Is there any solution which would still adhere to OOP principles?</p>
  
  <p><strong>Edit</strong>
  I am aware that PHP does not require type hinting at all but it is perhaps bad OOP. Firstly it is not obvious from the interface (the method signature) what kind of objects should be accepted. So if another developer wanted to use the method it should be obvious that objects of type X are required without them having to look through the implementation (method body) which is bad encapsulation and breaks the information hiding principle. Secondly because there's no type safety the method can accept any invalid variable which means manual type checking and exception throwing is needed all over the place!</p>