Fatal error: Uncaught TypeError when returning class instead of interface

18 Views Asked by At

I ran into a bizarre issue with PHP type system. I have the following interface and class:

interface IDB { ... }

class DB implements IDB { ... }

I then try to return an instance of DB class like this:

public function GetDB(): IDB {
    return new DB();
}

This works as expected on my local Wampserver. However, when I uploaded this to my hosting provider (which uses PHP 8.2), I get the following error:

Fatal error: Uncaught TypeError: GetDB(): Return value must be of type IDB, DB returned

Have been going back and forth for a while now and I'm really confused about this error. Any help would be appreciated. Thanks.

0

There are 0 best solutions below