【发布时间】:2015-09-20 21:42:42
【问题描述】:
我在实现接口的类上遇到了一个奇怪的错误。
错误:
可捕获的致命错误:参数 1 传递给 MyApp\Library\Cache::__construct() 必须是 MyApp\Contacts\CacheInterface,MyApp\Driver\Cache\File 的实例 给定
文件类:
namespace MyApp\Driver\Cache;
use MyApp\Library\Config;
use MyApp\Contracts\CacheInterface;
class File implements CacheInterface {
private $expire;
public function __construct($expire, Config $config) {
$this->expire = $expire;
$this->config = $config;
... more code
}
}
缓存类:
namespace MyApp\Library;
use MyApp\Contacts\CacheInterface;
final class Cache {
private $cache;
public function __construct(CacheInterface $cache) {
$this->cache = $cache;
}
... more methods
}
界面:
namespace MyApp\Contracts;
interface CacheInterface {
public function get($key);
public function set($key, $value);
public function delete($key);
public function flush_cache();
}
在 Pimple Container 中作为服务实现,如下所示:
$this->data['cache'] = function ($data) {
switch ($data['config_cache_type_id']):
case 'apc':
$driver = new Apc($data['cache.time'], $data['config']);
break;
case 'mem':
$driver = new Mem($data['cache.time'], $data['config']);
$driver->connect();
break;
case 'file':
default:
$driver = new File($data['cache.time'], $data['config']);
break;
endswitch;
return new Cache($driver);
};
当然,这 4 个 Driver 和 Cache 类包含在容器类之前的 use 关键字中。
我看不出我缺少什么我已经以完全相同的程序完成了其他几份合同。任何想法将不胜感激。
【问题讨论】:
-
是的,很抱歉我忘了提。