Baza znanjaIzpitno središče
SLSlovenščina
Izberi jezik13

CKTIS / PHP

ArgumentCountError

ArgumentCountError deduje od TypeError.

Različica: PHP 8.4; repository runtime, production runtime not attested

Sintaksa

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

ArgumentCountError deduje od TypeError. V PHP 8.4 se pojavi ob manjkajočem obveznem argumentu uporabniške funkcije ali odvečnih položajnih argumentih nevariadične vgrajene funkcije. Uporabniška funkcija lahko dodatne položajne argumente prebere s func_get_args. Brez obveznega argumenta se telo first ne izvede.

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

Primeri

<?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

Viri