在PHP中应用稳定抽象原理

phpMetrics的建议是利用“稳定抽象原理”并列出一些类。

这些类非常简单,并实现了PSR SimpleCache

如何将原理正确地应用于此类?

use Psr\SimpleCache\CacheInterface;
use stdClass;

class Filecache implements CacheInterface
{
    /**
     * @var stdClass
     */
    protected $obj;

    /**
     * @var string Path to the cache file
     */
    protected $file;

    /**
     * @param string $cacheFile
     */
    public function __construct($cacheFile)
    {
        $this->file = $cacheFile;
        if (!file_exists($this->file)) {
            touch($this->file);
        }
        $this->obj = json_decode(file_get_contents($this->file));
    }

    /**
     *
     */
    public function __destruct()
    {
        $file = fopen($this->file,'w');
        fwrite($file,json_encode($this->obj));
        fclose($file);
    }

    /**
     * @inheritDoc
     */
    public function get($key,$default = null)
    {
        return isset($this->obj->{$key}) ? $this->obj->{$key} : $default;
    }

    /**
     * @inheritDoc
     */
    public function set($key,$value,$ttl = null)
    {
        if (!$this->obj instanceof stdClass) {
            $this->obj = new stdClass();
        }
        $this->obj->{$key} = $value;
    }

    /**
     * @inheritDoc
     */
    public function delete($key)
    {
        unset($this->obj->{$key});
    }

    /**
     * @inheritDoc
     */
    public function clear()
    {
        $this->obj = new stdClass();
        return true;
    }

    /**
     * @inheritDoc
     */
    public function getMultiple($keys,$default = null)
    {
        // TODO: Implement getMultiple() method.
    }

    /**
     * @inheritDoc
     */
    public function setMultiple($values,$ttl = null)
    {
        // TODO: Implement setMultiple() method.
    }

    /**
     * @inheritDoc
     */
    public function deleteMultiple($keys)
    {
        // TODO: Implement deleteMultiple() method.
    }

    /**
     * @inheritDoc
     */
    public function has($key)
    {
        return isset($this->obj->{$key});
    }
}
li512110 回答:在PHP中应用稳定抽象原理

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2671378.html

大家都在问