Interfejs ArrayAccess
ArrayAccess
&reftitle.intro;
Interfejs umożliwiający dostęp do obiektów, jak do tablic.
&reftitle.interfacesynopsis;
ArrayAccess
&Methods;
&reftitle.examples;
Podstawowe użycie
1,
"dwa" => 2,
"trzy" => 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["dwa"]));
var_dump($obj["dwa"]);
unset($obj["dwa"]);
var_dump(isset($obj["dwa"]));
$obj["dwa"] = "Wartość";
var_dump($obj["two"]);
$obj[] = 'Dodany 1';
$obj[] = 'Dodany 2';
$obj[] = 'Dodany 3';
print_r($obj);
?>
]]>
&example.outputs.similar;
Array
(
[raz] => 1
[trzy] => 3
[dwa] => Wartość
[0] => Dodane 1
[1] => Dodane 2
[2] => Dodane 3
)
)
]]>
&language.predefined.arrayaccess.offsetexists;
&language.predefined.arrayaccess.offsetget;
&language.predefined.arrayaccess.offsetset;
&language.predefined.arrayaccess.offsetunset;