I have a PHP interface and its concrete class as below.
interface InterfaceA
{
public function methodA(array $data): array;
}
class ClassA implements InterfaceA
{
public function methodA(array $data): array
{
// method stuffs
return [
'id' => 1,
'name' => 'Name 1'
];
}
}
I need a way to strictly define the return type (array) with its properties (id, name) in the interface method.
The intention of this is when someone implements the same interface for another concrete class (say ClassB), he might have missed some properties (like 'name') from the returned array.
Is there a way in PHP (I'm using PHP 8.0), to avoid this mistake?