我在實現接口的類上收到奇怪的錯誤。不返回接口實例的類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
關鍵字。
我看不到我在想什麼我在完全相同的過程中完成了其他幾個合同。任何想法,將不勝感激。
是他們這樣做,對不起,我忘了要提到這一點。 –