CKTIS / PHP
AssertionError
AssertionError je Error pri izvedbi neresnične trditve, če so izjeme trditev omogočene.
Različica: PHP 8.4; repository runtime, production runtime not attested
Sintaksa
assert($condition, "broken invariant")AssertionError je Error pri izvedbi neresnične trditve, če so izjeme trditev omogočene. Besedilni opis poda sporočilo, podani Throwable pa se sproži namesto nje. Izključena trditev lahko izpusti ovrednotenje izraza. Primer zažene ločen tolmač z omogočenimi trditvami.
function executeMode(int $mode, string $source): string {
$command = [PHP_BINARY, "-n", "-d", "zend.assertions=$mode", "-r", $source];
$process = proc_open($command, [1 => ["pipe", "w"], 2 => ["pipe", "w"]], $pipes);
if (!is_resource($process)) { throw new RuntimeException("start failed"); }
$answer = stream_get_contents($pipes[1]);
$error = stream_get_contents($pipes[2]);
fclose($pipes[1]); fclose($pipes[2]);
if (proc_close($process) !== 0) { throw new RuntimeException($error); }
return $answer;
}
$code = <<<'PHP'
try { assert(false, "invariant"); }
catch (AssertionError $e) { echo get_class($e), " ", $e->getMessage(), PHP_EOL; }
try { assert(false, new DomainException("custom")); }
catch (DomainException $e) { echo get_class($e); }
PHP;
echo executeMode(1, $code);
AssertionError invariant
DomainException
Primeri
<?php
function executeMode(int $mode, string $source): string {
$command = [PHP_BINARY, "-n", "-d", "zend.assertions=$mode", "-r", $source];
$process = proc_open($command, [1 => ["pipe", "w"], 2 => ["pipe", "w"]], $pipes);
if (!is_resource($process)) { throw new RuntimeException("start failed"); }
$answer = stream_get_contents($pipes[1]);
$error = stream_get_contents($pipes[2]);
fclose($pipes[1]); fclose($pipes[2]);
if (proc_close($process) !== 0) { throw new RuntimeException($error); }
return $answer;
}
$code = <<<'PHP'
try { assert(false, "invariant"); }
catch (AssertionError $e) { echo get_class($e), " ", $e->getMessage(), PHP_EOL; }
try { assert(false, new DomainException("custom")); }
catch (DomainException $e) { echo get_class($e); }
PHP;
echo executeMode(1, $code);
Rezultat
AssertionError invariant DomainException