PHP function polymorphism
<p>I would like to use function polymorphism in my <a href="http://en.wikipedia.org/wiki/PHP" rel="nofollow">PHP</a> classes. I intend to check the input type of a function for some of my children classes functions.</p>

<pre><code>class A
{
    public function setValue(Type $value)
    {
    }
}

class B extends A
{
    public function setValue(XType $value)
    {
    }
}

class C extends A
{
    public function setValue(ZType $value)
    {
    }
}
$b = new B();
$b-&gt;setValue(new XType());
</code></pre>

<p>with</p>

<pre><code>class Type {};
class XType extends Type;
class ZType extends Type;
</code></pre>

<p>The behavior I am looking for is that PHP throws an exception if <em>setValue</em> is called with the wrong argument type.</p>

<p>If I use the above code, I get an error:</p>

<blockquote>
  <p>Runtime Notice: Declaration of B::setValue() should be compatible with A::setValue(Type $value) in B.php line ..</p>
</blockquote>

<p>This is how I managed to obtain the behavior I wanted:</p>

<pre><code>class B extends A
{
    public function setValue(Type $value)
    {
        if (!$value instanceof XType) {
            throw new \Exception("value $value not allowed, should be of type XType");
        parent::setValue($value);
        }
    }
}
</code></pre>

<p>But I would be glad to let the PHP core handle that bit of code for me :)</p>

<p>I see that I was <strong>not</strong> clear enough..</p>

<p>I am trying to provide the caller with a default behavior, <em>A::setValue()</em>, and with a specific behaviour regarding certain child classes, <em>B::setValue()</em>, so if the caller tries to call <em>B::setValue()</em> with an unsupported type, it will get an error, but if it calls, let's say, <em>D::setValue()</em>, it will get the default, <em>A::setValue()</em>.</p>
