CKTIS / PHP
OutOfBoundsException
OutOfBoundsException deriva de RuntimeException para un índice o clave inválidos.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
throw new OutOfBoundsException("index missing")OutOfBoundsException deriva de RuntimeException para un índice o clave inválidos. Una colección puede lanzarla al comprobar límites, mientras leer una clave ausente de un array ordinario en PHP 8.4 emite un aviso y da null. La función array_key_exists distingue null almacenado de una clave ausente.
function fetchRow(array $rows, int $i): mixed {
if (!array_key_exists($i, $rows)) { throw new OutOfBoundsException("index " . $i); }
return $rows[$i];
}
var_dump(fetchRow([null], 0));
try { fetchRow([null], 1); }
catch (OutOfBoundsException $e) { echo get_class($e), " ", $e->getMessage(); }
NULL
OutOfBoundsException index 1
Ejemplos
<?php
function fetchRow(array $rows, int $i): mixed {
if (!array_key_exists($i, $rows)) { throw new OutOfBoundsException("index " . $i); }
return $rows[$i];
}
var_dump(fetchRow([null], 0));
try { fetchRow([null], 1); }
catch (OutOfBoundsException $e) { echo get_class($e), " ", $e->getMessage(); }
Resultado
NULL OutOfBoundsException index 1