Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

ArgumentCountError

ArgumentCountError deriva da TypeError.

Versione: PHP 8.4; repository runtime, production runtime not attested

Sintassi

try { strlen("a", "b"); } catch (ArgumentCountError $e) { /* ... */ }

ArgumentCountError deriva da TypeError. In PHP 8.4 si verifica per un argomento obbligatorio mancante in una funzione utente o argomenti posizionali in eccesso in una funzione interna non variadica. Una funzione utente può leggere gli argomenti posizionali aggiuntivi con func_get_args. Senza l’argomento richiesto il corpo di first non viene eseguito.

function first($value) { return count(func_get_args()); }
foreach ([fn() => first(), fn() => strlen("x", "y")] as $call) {
    try { $call(); } catch (ArgumentCountError $e) {
        echo get_class($e), " ", ($e instanceof TypeError ? "TypeError" : "other"), PHP_EOL;
    }
}
echo first("A", "B");
ArgumentCountError TypeError
ArgumentCountError TypeError
2

Esempi

<?php
function first($value) { return count(func_get_args()); }
foreach ([fn() => first(), fn() => strlen("x", "y")] as $call) {
    try { $call(); } catch (ArgumentCountError $e) {
        echo get_class($e), " ", ($e instanceof TypeError ? "TypeError" : "other"), PHP_EOL;
    }
}
echo first("A", "B");

Risultato

ArgumentCountError TypeError
ArgumentCountError TypeError
2

Fonti