Baza znanjaCentar za ispite
HRHrvatski
Odaberi jezik13

CKTIS / PHP

ArgumentCountError

ArgumentCountError nasljeđuje TypeError.

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

Sintaksa

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

ArgumentCountError nasljeđuje TypeError. U PHP-u 8.4 javlja se pri izostanku obveznog argumenta korisničke funkcije ili suvišku položajnih argumenata interne funkcije koja nije varijadna. Korisnička funkcija može čitati dodatne položajne argumente s func_get_args. Bez obveznog argumenta tijelo first se ne izvršava.

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

Primjeri

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

Rezultat

ArgumentCountError TypeError
ArgumentCountError TypeError
2

Izvori