[2020 新春红包题]1
[2020 新春红包题]1
考点
wp
class A {
protected $store;
protected $key;
protected $expire;
public function __construct($store, $key = 'flysystem', $expire = null) {
$this->key = $key;
$this->store = $store;
$this->expire = $expire;
}
public function cleanContents(array $contents) {
$cachedProperties = array_flip([
'path', 'dirname', 'basename', 'extension', 'filename',
'size', 'mimetype', 'visibility', 'timestamp', 'type',
]); //array(10) { ["path"]=> int(0) ["dirname"]=> int(1) ["basename"]=> int(2) ["extension"]=> int(3) ["filename"]=> int(4) ["size"]=> int(5) ["mimetype"]=> int(6) ["visibility"]=> int(7) ["timestamp"]=> int(8) ["type"]=> int(9) }
// 2.如果是二维数组,进入if,否则直接返回
foreach ($contents as $path => $object) {
if (is_array($object)) {
$contents[$path] = array_intersect_key($object, $cachedProperties);
}
}
return $contents;
}
public function getForStorage() {
// cache是个数组,cleaned也是个数组
$cleaned = $this->cleanContents($this->cache);
// 3. 把数组转成json字符串返回
return json_encode([$cleaned, $this->complete]);
}
public function save() {
$contents = $this->getForStorage();
// 4.store是B类
$this->store->set($this->key, $contents, $this->expire);
}
public function __destruct() {
if (!$this->autosave) { // 1.autosave=false
$this->save();
}
}
}第一种做法
第二种做法
第三种做法
小结
最后更新于