我在实现接口的类上遇到了一个奇怪的错误。
错误:
可捕获的致命错误:参数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容器中实现为服务,如下所示:
$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);
};
当然,容器类之前的use
关键字包含4个Driver和Cache类。
我无法看到我错过的东西我在完全相同的程序中完成了其他几项合同。任何想法都将不胜感激。
答案 0 :(得分:1)
在你的File.class中,你可以尝试替换:
use MyApp\Contracts\CacheInterface;
使用
use MyApp\Contacts\CacheInterface;
对于您的界面,请使用:
namespace MyApp\Contacts;