簡單的就地資料包裝器
interface IArrayWrapper {
public function getProperties(): array;
public function has(string $name): bool;
public function __toString();
// ...
};
/**
* Lightweight in-place data wrapper.
* Demonstrates usage of anonymous class in conjunction with interface.
*
* Provides some basic functionality for managing array data in OO style.
* Can be used as a wrapper for API request/response data etc.
* Converts data to JSON with simple `(string)` cast.
*/
new class($data) implements IArrayWrapper
{
/** @var array */
private $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function getProperties(): array
{
return is_array($this->data) ? array_keys($this->data) : [] ;
}
public function has(string $name): bool
{
return (bool)($this->data[$name] ?? false);
}
public function get(string $name)
{
return $this->data[$name] ?? null;
}
public function __isset($name)
{
return $this->has($name);
}
public function __get($name)
{
return $this->get($name);
}
public function __toString()
{
return json_encode($this->data);
}
};
用法
假設我們的 $data
如下,並且類儲存在 $cls
變數中:
$data = ['a' => 'b', 'c' => 'd', 'e' => 5];
$cls->a; // b
$cls->b; // null
$cls->e; // 5
isset($cls->a); // true
isset($cls->b); // false
$cls->has('a'); // true
$cls->has('b'); // false
$cls->getProperties(); // Array([0] => a [1] => c [2] => e)
(string)$cls; // {"a":"b","c":"d","e":5}
$cls instanceof IArrayWrapper; // true