База знанияИзпитен център
BGБългарски
Избери език13

CKTIS / PHP

zend.assertions

Директивата zend.assertions управлява генерирането и изпълнението на код за assert.

Версия: PHP 8.4; repository runtime, production runtime not attested

Синтаксис

php -d zend.assertions=1 script.php

Директивата zend.assertions управлява генерирането и изпълнението на код за assert. Режим 1 генерира и изпълнява, 0 генерира и пропуска изпълнението, а -1 не генерира код за твърдението. Преминаване към или от -1 изисква настройка при стартиране. Три CLI процеса показват кога възниква страничният ефект.

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

Примери

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

Резултат

1 1
0 0
-1 0

Източници