CKTIS / PHP
InvalidArgumentException
InvalidArgumentException is een LogicException die code expliciet kan werpen voor een argument van ongeschikt type.
Versie: PHP 8.4; repository runtime, production runtime not attested
Syntax
throw new InvalidArgumentException("string label required")InvalidArgumentException is een LogicException die code expliciet kan werpen voor een argument van ongeschikt type. Een gedeclareerd parametertype kan afzonderlijk een TypeError van de engine geven. De functie label accepteert mixed en weigert het getal zelf, terwijl typed het met strict_types weigert.
declare(strict_types=1);
function label(mixed $x): string {
if (!is_string($x)) { throw new InvalidArgumentException("string label required"); }
return strtoupper($x);
}
function typed(string $x): string { return $x; }
echo label("ok"), PHP_EOL;
foreach ([fn() => label(3), fn() => typed(3)] as $call) {
try { $call(); } catch (Throwable $e) { echo get_class($e), PHP_EOL; }
}
OK
InvalidArgumentException
TypeError
Voorbeelden
<?php
declare(strict_types=1);
function label(mixed $x): string {
if (!is_string($x)) { throw new InvalidArgumentException("string label required"); }
return strtoupper($x);
}
function typed(string $x): string { return $x; }
echo label("ok"), PHP_EOL;
foreach ([fn() => label(3), fn() => typed(3)] as $call) {
try { $call(); } catch (Throwable $e) { echo get_class($e), PHP_EOL; }
}
Resultaat
OK InvalidArgumentException TypeError