Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / PHP

ArgumentCountError

ArgumentCountError dedí od TypeError.

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

Syntax

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

ArgumentCountError dedí od TypeError. V PHP 8.4 vzniká pri chýbajúcom povinnom argumente používateľskej funkcie alebo nadbytočných pozičných argumentoch nevariadickej vstavanej funkcie. Používateľská funkcia môže ďalšie pozičné argumenty čítať cez func_get_args. Bez povinného argumentu sa telo 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

Prí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ýsledok

ArgumentCountError TypeError
ArgumentCountError TypeError
2

Zdroje