I’m using Type Hinting on a class that gets passed an object into its constructor. The passed object (Bar) exists in its own namespace. This is all fine until I swap out the passed object with a fake one (for unit testing purposes). My test Bar object resides in its own namespace and now an error is (quite rightly) thrown.
use Some\Place\Bar
class Foo {
// Passing a test Bar into here now throws error.
public function __construct(Bar $bar) {
$this->bar = $bar;
}
}
I’m new to PHP so unsure on what is possible. Is there a way round this or do I just need to drop Type Hinting from the class (which is my current solution)?
2 of the several options :
pass an object in the constructor that implement an interface IBar :
or dont use type hinting
the point is , the signature of a method should never depend on an implementation , always an interface.
EDIT : so you just need to import the interface then the fake class wherever it comes from. FakeBar can then mock Bar.