【发布时间】:2014-08-14 14:11:49
【问题描述】:
我在构造函数中有一个类型提示,但由于某种原因我得到了
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();
}
处理程序.php
namespace Log;
require_once 'TestInterface.php';
use Log\TestInterface;
class Handler implements TestInterface
{
public function log()
{
//some logic goes here
}
}
日志.php
require_once 'Handler.php';
use Log\Handler;
class Log
{
public function __construct(TestInterface $handler)
{
$handler->log('test');
}
}
$obj = new Log(new Handler());
那么为什么我会收到这个错误?我认为当我实现 TestInterface 时,Handler 类的实例将通过 Log 构造函数传递。
【问题讨论】: