我在实现接口的类上收到奇怪的错误。不返回接口实例的类PHP
错误:
Catchable fatal error: Argument 1 passed to MyApp\Library\Cache::__construct() must be an instance of MyApp\Contacts\CacheInterface, instance of MyApp\Driver\Cache\File given
文件类:
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();
}
实现为一个疙瘩容器中的服务,像这样:
$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个驱动和高速缓存类都包含与容器类前use
关键字。
我看不到我在想什么我在完全相同的过程中完成了其他几个合同。任何想法,将不胜感激。
是他们这样做,对不起,我忘了要提到这一点。 –