samedi 22 décembre 2018

Should concrete class follow the type hint of its interface?

In this code sample, the interface doesn't seem to care whether the implementing method foo() checks for an array type parameter even if it explicitly type-hinted array only

<?php

declare(strict_types = 1);

interface MyInterface
{
    public function foo(array $foo);
}


class Bar implements MyInterface
{
    public function foo($foo)
    {
        return $foo;
    }
}


echo (new Bar)->foo('test'); // runs just fine as string

I'm expecting at least a fatal, incompatible interface error but there's none.

My questions are:

  1. Is this an expected behavior?
  2. Should the interface not have type hints at all because it's not respect anyway?

Aucun commentaire:

Enregistrer un commentaire