CKTIS / PHP
AssertionError
AssertionError е Error при изпълнение на невярно твърдение, когато изключенията за твърдения са включени.
Версия: PHP 8.4; repository runtime, production runtime not attested
Синтаксис
assert($condition, "broken invariant")AssertionError е Error при изпълнение на невярно твърдение, когато изключенията за твърдения са включени. Текстово описание задава съобщението, а подаден Throwable се хвърля вместо него. Изключено твърдение може да пропусне изчисляването на израза. Примерът стартира отделен интерпретатор с включени твърдения.
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
Примери
<?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);
Резултат
AssertionError invariant DomainException