<?hhclass Obj<string, string> implements ArrayAccess<string, string> {
private array $container = array();
public function __construct() {
$this->container = array(
"one" => 1,
"two" => 2,
"three" => 3,
);
}
public function offsetSet(string $key, string $value): this {
if (is_null($key)) {
$this->container[] = $value;
} else {
$this->container[$key] = $value;
}
}
public function offsetExists(string $offset): bool {
return isset($this->container[$offset]);
}
public function offsetUnset(string $offset): this {
unset($this->container[$offset]);
}
public function offsetGet(string $offset): ?string {
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}$obj = new Obj();var_dump(isset($obj["two"]));var_dump($obj["two"]);
unset($obj["two"]);var_dump(isset($obj["two"]));$obj["two"] = "A value";var_dump($obj["two"]);$obj[] = 'Append 1';$obj[] = 'Append 2';$obj[] = 'Append 3';print_r($obj);
The above example will output something similar to:
bool(true)
int(2)
bool(false)
string(7) "A value"
obj Object
(
[container:obj:private] => Array
(
[one] => 1
[three] => 3
[two] => A value
[0] => Append 1
[1] => Append 2
[2] => Append 3
)
)