Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / PHP

ArgumentCountError

ArgumentCountError dědí od TypeError.

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

Syntaxe

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

ArgumentCountError dědí od TypeError. V PHP 8.4 vzniká při chybějícím povinném argumentu uživatelské funkce či nadbytečných pozičních argumentech nevariadické vestavěné funkce. Uživatelská funkce může další poziční argumenty číst přes func_get_args. Bez povinného argumentu se tělo first nespustí.

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

Příklady

<?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");

Výsledek

ArgumentCountError TypeError
ArgumentCountError TypeError
2

Zdroje