CKTIS / PHP
zend.assertions
A diretiva zend.assertions controla a geração e execução do código assert.
Versão: PHP 8.4; repository runtime, production runtime not attested
Sintaxe
php -d zend.assertions=1 script.phpA diretiva zend.assertions controla a geração e execução do código assert. O modo 1 gera e executa, 0 gera e ignora na execução e -1 não gera código de asserção. Passar para -1 ou sair desse modo exige configuração no arranque. Três processos CLI mostram quando ocorre o efeito secundário.
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
Exemplos
<?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;
}
Resultado
1 1 0 0 -1 0