Reputation: 305
I have a type hint in the constructor but for some reason I get
Catchable fatal error: Argument 1 passed to Log::__construct() must be an instance of TestInterface, instance of Log\Handler given, called in
TestInterface.php
namespace Log;
interface TestInterface
{
public function log();
}
Handler.php
namespace Log;
require_once 'TestInterface.php';
use Log\TestInterface;
class Handler implements TestInterface
{
public function log()
{
//some logic goes here
}
}
Log.php
require_once 'Handler.php';
use Log\Handler;
class Log
{
public function __construct(TestInterface $handler)
{
$handler->log('test');
}
}
$obj = new Log(new Handler());
So why do I get this error? I thought that when I implement the TestInterface the instance of Handler class will be passed through the Log constructor.
Upvotes: 2
Views: 56
Reputation: 3873
You have to add use Log\TestInterface;
to the top of Log.php, because TestInterface is undefined in the global namespace. What you got back was not a helpful error message at all, but this should do it:
require_once 'Handler.php';
use Log\Handler;
use Log\TestInterface;
class Log
{
public function __construct(TestInterface $handler)
{
$handler->log('test');
}
}
$obj = new Log(new Handler());
Upvotes: 2