CKTIS / PHP
Parola chiave throw
Lancia un oggetto Throwable e passa il controllo al gestore compatibile o a un livello superiore dello stack.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
$item = $map[$key] ?? throw new OutOfBoundsException();Lancia un oggetto Throwable e passa il controllo al gestore compatibile o a un livello superiore dello stack. Da PHP 8 throw è un’espressione e può gestire un valore mancante dopo ??. Il lancio interrompe la valutazione su quel percorso.
function requireKey(array $row){return $row["id"] ?? throw new OutOfBoundsException("missing");}
echo requireKey(["id"=>0])," "; try{requireKey([]);}catch(OutOfBoundsException $e){echo $e->getMessage();}
0 missing
Esempi
<?php
function requireKey(array $row){return $row["id"] ?? throw new OutOfBoundsException("missing");}
echo requireKey(["id"=>0])," "; try{requireKey([]);}catch(OutOfBoundsException $e){echo $e->getMessage();}
Risultato
0 missing