La interfaz ArrayAccess
ArrayAccess
&reftitle.intro;
Interfaz para proporcionar acceso a objetos como arrays.
&reftitle.interfacesynopsis;
ArrayAccess
ArrayAccess
&Methods;
Uso básico
contenedor = array(
"uno" => 1,
"dos" => 2,
"tres" => 3,
);
}
public function offsetSet($offset, $valor) {
if (is_null($offset)) {
$this->contenedor[] = $valor;
} else {
$this->contenedor[$offset] = $valor;
}
}
public function offsetExists($offset) {
return isset($this->contenedor[$offset]);
}
public function offsetUnset($offset) {
unset($this->contenedor[$offset]);
}
public function offsetGet($offset) {
return isset($this->contenedor[$offset]) ? $this->contenedor[$offset] : null;
}
}
$obj = new obj;
var_dump(isset($obj["dos"]));
var_dump($obj["dos"]);
unset($obj["dos"]);
var_dump(isset($obj["dos"]));
$obj["dos"] = "Un valor";
var_dump($obj["dos"]);
$obj[] = 'Añadido 1';
$obj[] = 'Añadido 2';
$obj[] = 'Añadido 3';
print_r($obj);
?>
]]>
&example.outputs.similar;
Array
(
[uno] => 1
[tres] => 3
[dos] => Un valor
[0] => Añadido 1
[1] => Añadido 2
[2] => Añadido 3
)
)
]]>
&language.predefined.arrayaccess.offsetexists;
&language.predefined.arrayaccess.offsetget;
&language.predefined.arrayaccess.offsetset;
&language.predefined.arrayaccess.offsetunset;