Das ArrayAccess-Interface
ArrayAccess
&reftitle.intro;
Interface, um Objekte als Arrays ansprechen zu können
&reftitle.interfacesynopsis;
ArrayAccess
&Methods;
&reftitle.examples;
Grundlegende Verwendung
1,
"zwei" => 2,
"drei" => 3,
];
public function offsetSet($offset, $value): void {
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset): bool {
return isset($this->container[$offset]);
}
public function offsetUnset($offset): void {
unset($this->container[$offset]);
}
public function offsetGet($offset): mixed {
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}
$obj = new Obj;
var_dump(isset($obj["zwei"]));
var_dump($obj["zwei"]);
unset($obj["zwei"]);
var_dump(isset($obj["zwei"]));
$obj["zwei"] = "Ein Wert";
var_dump($obj["zwei"]);
$obj[] = 'Anhängen 1';
$obj[] = 'Anhängen 2';
$obj[] = 'Anhängen 3';
print_r($obj);
?>
]]>
&example.outputs.similar;
Array
(
[eins] => 1
[drei] => 3
[zwei] => Ein Wert
[0] => Anhängen 1
[1] => Anhängen 2
[2] => Anhängen 3
)
)
]]>
&language.predefined.arrayaccess.offsetexists;
&language.predefined.arrayaccess.offsetget;
&language.predefined.arrayaccess.offsetset;
&language.predefined.arrayaccess.offsetunset;