Introduction
PHP Manual
PHP Manual»Structures de données»SplFixedArray

La classe SplFixedArray

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

La classe SplFixedArray fournit les fonctionnalités principales d'un tableau. La différence majeure entre un objet SplFixedArray et un array standard de PHP est que SplFixedArray doit être redimensionné manuellement, et n'utilise que des int dans cette plage pour les index. L'avantage est qu'il utilise moins de mémoire qu'un array standard.

class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable, JsonSerializable {
/* Méthodes */
public function __construct(int $size = 0)
public function count(): int
public function current(): mixed
public static function fromArray(array $array, bool $preserveKeys = true): SplFixedArray
public function getIterator(): Iterator
public function getSize(): int
public function jsonSerialize(): array
public function key(): int
public function next(): void
public function offsetExists(int $index): bool
public function offsetGet(int $index): mixed
public function offsetSet(int $index, mixed $value): void
public function offsetUnset(int $index): void
public function rewind(): void
public function __serialize(): array
public function setSize(int $size): true
public function toArray(): array
public function __unserialize(array $data): void
public function valid(): bool
#[\Deprecated]
public function __wakeup(): void
}
Version Description
8.4.0 Les accès hors limites dans SplFixedArray lèvent désormais des exceptions de type OutOfBoundsException au lieu de RuntimeException. Comme OutOfBoundsException est une classe fille de RuntimeException, aucun changement de comportement n'apparaît lors de la capture de ces exceptions.
8.2.0 Les méthodes magiques SplFixedArray::__serialize() et SplFixedArray::__unserialize() ont été ajoutées à SplFixedArray.
8.1.0 La classe SplFixedArray implémente désormais JsonSerializable.
8.0.0 La classe SplFixedArray implémente désormais IteratorAggregate. Auparavant, Iterator était implémentée.

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

To Top