(PHP 5 >= 5.3.0, PHP 7, PHP 8)
Introduction
La classe SplFixedArray fournit les fonctionnalités
principales d'un tableau. La différence majeure entre un objet
SplFixedArray et un tableau standard de PHP est
que SplFixedArray doit être redimensionné manuellement, et
n'utilise que des entier dans cette plage pour les index. L'avantage est qu'il utilise
moins de mémoire qu'un tableau standard.
Synopsis de la classe
class
SplFixedArray
implements
IteratorAggregate, ArrayAccess, Countable, JsonSerializable {
/* Méthodes */
public __construct(int
$size = 0)
public
count(): int
public
current(): mixed
public static fromArray(array
$array, bool
$preserveKeys =
true): SplFixedArray
public getIterator(): Iterator
public
getSize(): int
public jsonSerialize(): mixed
public
key(): int
public
next(): void
public offsetExists(int
$index): bool
public offsetGet(int
$index): mixed
public offsetSet(int
$index, mixed
$value): void
public offsetUnset(int
$index): void
public
rewind(): void
public __serialize(): array
public setSize(int
$size): bool
public
toArray(): array
public __unserialize(array
$data): void
public
valid(): bool
public
__wakeup(): void
}
Historique
Exemples
Exemple #1 Exemple avec SplFixedArray
<?php
// Initialisation d'un tableau avec une taille fixe
$array = new SplFixedArray(5);
$array[1] = 2;
$array[4] = "foo";
var_dump($array[0]); // NULL
var_dump($array[1]); // int(2)
var_dump($array["4"]); // string(3) "foo"
// Augmentation de la taille à 10
$array->setSize(10);
$array[9] = "asdf";
// Réduction de taille de 2
$array->setSize(2);
// Les lignes suivantes émettent une RuntimeException : index invalide ou hors de l'intervalle
try {
var_dump($array["non-numeric"]);
} catch(RuntimeException $re) {
echo "RuntimeException : ".$re->getMessage()."\n";
}
try {
var_dump($array[-1]);
} catch(RuntimeException $re) {
echo "RuntimeException : ".$re->getMessage()."\n";
}
try {
var_dump($array[5]);
} catch(RuntimeException $re) {
echo "RuntimeException : ".$re->getMessage()."\n";
}
?>
L'exemple ci-dessus va afficher :
NULL
int(2)
string(3) "foo"
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
RuntimeException: Index invalid or out of range
Sommaire
- SplFixedArray::__construct — Construit un nouveau SplFixedArray
- SplFixedArray::count — Retourne la taille du tableau à taille fixe
- SplFixedArray::current — Lit l'élément courant du tableau à taille fixe
- SplFixedArray::fromArray — Importe un tableau PHP dans un tableau à taille fixe
- SplFixedArray::getIterator — Retrieve the iterator to go through the array
- SplFixedArray::getSize — Lit la taille du tableau à taille fixe
- SplFixedArray::jsonSerialize — Returns a representation that can be converted to JSON
- SplFixedArray::key — Retourne l'index de la position courante
- SplFixedArray::next — Déplace l'itérateur à l'élément suivant
- SplFixedArray::offsetExists — Vérifie si l'index demandé existe
- SplFixedArray::offsetGet — Retourne la valeur à l'index donné
- SplFixedArray::offsetSet — Affecte une valeur à un index donné
- SplFixedArray::offsetUnset — Détruit l'élément à un index donné
- SplFixedArray::rewind — Remet l'itérateur au début du tableau à taille fixe
- SplFixedArray::__serialize — Serializes the SplFixedArray object
- SplFixedArray::setSize — Change la taille d'un tableau à taille fixe
- SplFixedArray::toArray — Retourne un tableau PHP à partir d'un tableau à taille fixe
- SplFixedArray::__unserialize — Deserializes the data parameter into an SplFixedArray object
- SplFixedArray::valid — Vérifie si le tableau contient encore un élément
- SplFixedArray::__wakeup — Ré-initialise le tableau après sa dé-linéarisation