Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / PHP

zend.assertions

Direktiva zend.assertions řídí generování a provádění kódu assert.

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

Syntaxe

php -d zend.assertions=1 script.php

Direktiva zend.assertions řídí generování a provádění kódu assert. Režim 1 generuje a provádí, 0 generuje a přeskočí provedení a -1 kód aserce nevygeneruje. Přechod do -1 nebo z něj vyžaduje nastavení při spuštění. Tři procesy CLI ukazují, kdy nastane vedlejší účinek.

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;
}
foreach ([1, 0, -1] as $mode) {
    $code = '$n = 0; assert(++$n === 1); echo $n;';
    echo $mode, " ", executeMode($mode, $code), PHP_EOL;
}
1 1
0 0
-1 0

Příklady

<?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;
}
foreach ([1, 0, -1] as $mode) {
    $code = '$n = 0; assert(++$n === 1); echo $n;';
    echo $mode, " ", executeMode($mode, $code), PHP_EOL;
}

Výsledek

1 1
0 0
-1 0

Zdroje