<?php
declare(strict_types=1);
$cases = [];
// Case 1: Guarding fopen() with file_exists() before opening
$missingPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mbsky_nonexistent_' . uniqid('', true) . '.txt';
$guardMessage = !file_exists($missingPath) ? '文件不存在' : 'opened';
$tmpFile = tempnam(sys_get_temp_dir(), 'mbsky_err_');
file_put_contents($tmpFile, 'hello-php-error');
$openedContent = '';
if (file_exists($tmpFile)) {
$fh = fopen($tmpFile, 'r');
if ($fh !== false) {
$openedContent = (string)fgets($fh);
fclose($fh);
}
}
@unlink($tmpFile);
$actual1 = [
'missing_exists' => file_exists($missingPath),
'guard_message' => $guardMessage,
'existing_read' => $openedContent,
];
$expected1 = [
'missing_exists' => false,
'guard_message' => '文件不存在',
'existing_read' => 'hello-php-error',
];
$cases[] = [
'name' => 'file_exists_guard_before_fopen',
'passed' => $actual1 === $expected1,
'expected' => $expected1,
'actual' => $actual1,
];
// Case 2: set_error_handler() capturing undefined variable (E_WARNING=2 in PHP 8+)
$captured = [];
set_error_handler(static function (int $errno, string $errstr, string $errfile, int $errline) use (&$captured): bool {
$captured[] = [
'errno' => $errno,
'errstr' => $errstr,
'has_file' => $errfile !== '',
'line_positive' => $errline > 0,
];
return true;
});
$triggerUndefined = static function () {
/** @noinspection PhpUndefinedVariableInspection */
return $undefinedTestVar;
};
$val = $triggerUndefined();
restore_error_handler();
$actual2 = [
'captured_count' => count($captured),
'errno' => $captured[0]['errno'] ?? -1,
'is_e_warning' => ($captured[0]['errno'] ?? -1) === E_WARNING,
'mentions_var' => str_contains($captured[0]['errstr'] ?? '', 'Undefined variable $undefinedTestVar'),
'has_file_and_line' => ($captured[0]['has_file'] ?? false) && ($captured[0]['line_positive'] ?? false),
'returned_null' => $val === null,
];
$expected2 = [
'captured_count' => 1,
'errno' => 2,
'is_e_warning' => true,
'mentions_var' => true,
'has_file_and_line' => true,
'returned_null' => true,
];
$cases[] = [
'name' => 'custom_error_handler_undefined_var',
'passed' => $actual2 === $expected2,
'expected' => $expected2,
'actual' => $actual2,
];
// Case 3: Error level constants & trigger_error() with E_USER_NOTICE / E_USER_WARNING
$triggered = [];
set_error_handler(static function (int $errno, string $errstr) use (&$triggered): bool {
$triggered[] = "[$errno] $errstr";
return true;
}, E_USER_WARNING | E_USER_NOTICE);
$test = 2;
if ($test > 1) {
trigger_error('变量值必须小于等于 1', E_USER_NOTICE);
trigger_error('变量值必须小于等于 1', E_USER_WARNING);
}
restore_error_handler();
$actual3 = [
'constants' => [
'E_WARNING' => E_WARNING,
'E_NOTICE' => E_NOTICE,
'E_USER_ERROR' => E_USER_ERROR,
'E_USER_WARNING' => E_USER_WARNING,
'E_USER_NOTICE' => E_USER_NOTICE,
'E_RECOVERABLE_ERROR' => E_RECOVERABLE_ERROR,
'E_ALL' => E_ALL,
],
'triggered' => $triggered,
];
$expected3 = [
'constants' => [
'E_WARNING' => 2,
'E_NOTICE' => 8,
'E_USER_ERROR' => 256,
'E_USER_WARNING' => 512,
'E_USER_NOTICE' => 1024,
'E_RECOVERABLE_ERROR' => 4096,
'E_ALL' => 30719,
],
'triggered' => [
'[1024] 变量值必须小于等于 1',
'[512] 变量值必须小于等于 1',
],
];
$cases[] = [
'name' => 'trigger_error_levels_and_constants',
'passed' => $actual3 === $expected3,
'expected' => $expected3,
'actual' => $actual3,
];
// Case 4: error_log() writing structured entry to a local log file (message_type = 3)
$logFile = tempnam(sys_get_temp_dir(), 'mbsky_errlog_');
$logWritten = false;
set_error_handler(static function (int $errno, string $errstr) use ($logFile, &$logWritten): bool {
$logWritten = error_log("Error: [$errno] $errstr\n", 3, $logFile);
return true;
}, E_USER_WARNING);
trigger_error('变量值必须小于等于 1', E_USER_WARNING);
restore_error_handler();
$logContent = trim((string)file_get_contents($logFile));
@unlink($logFile);
$actual4 = [
'log_written' => $logWritten,
'log_content' => $logContent,
];
$expected4 = [
'log_written' => true,
'log_content' => 'Error: [512] 变量值必须小于等于 1',
];
$cases[] = [
'name' => 'error_log_to_temp_file',
'passed' => $actual4 === $expected4,
'expected' => $expected4,
'actual' => $actual4,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-error-lab/explore-error.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-error.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1241.zip